Legacy Knowledge Base
公開されました Jun. 30, 2025

SPA が有効になっていると、Javascript コードが 2 回レンダリングされる

Written By

Daniel Couso

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack" publication program, made available for informational purposes. Articles in this program were published without a requirement for independent editing or verification and are provided"as is" without guarantee.

Before using any information from this article, independently verify its suitability for your situation and project.

問題

  • 次のコードは、DOM コンテンツが読み込まれるときにモーダル ウィンドウをレンダリングします。
...

if (document.readyState === 'loading'){
document.addEventListener("DOMContentLoaded", onContentLoaded);
} else {
onContentLoaded();
}

...
  • サイトのページを移動します。
  • メソッド onContentLoaded 2 回以上実行されるため、モーダル SPA もイベントDOMContentLoaded を使用してそれ自体をロードします。
  • カスタムコードのロード後に Liferay SPA がロードされた場合、 Liferay SPA ロジックがこの時点で存在する要素でページを再レンダリングするため、モーダルが2回表示されます。
  • これは、カスタム コードに既にマークアップが含まれ、そのイベントが登録されている場合、 Liferay SPA はそれらを削除し、それがまだ新しいナビゲーションであると見なすため、それらを再度ロードするために発生します。

Environment

  • DXP 7.0+

解決策

  • このような状況を防ぐために、 Liferay SPA が完全にロードされた後にカスタム コードが実行されるように調整する必要があります。 次の方法が考えられます。
if (Liferay && Liferay.SPA) {
Liferay.on('SPAReady', function() {
// The custom logic
});
}
  • この例を有効にするには、ファイル portal_normal.ftlの <@liferay_util["include"] page=top_head_include />行の後にカスタム コードを配置する必要があります。 これが保証されない場合、Liferay オブジェクトが定義されていない可能性があります。
Did this article resolve your issue ?

Legacy Knowledge Base