Issue
We have upgraded from Liferay 6.2 to DXP 7.4.
We have problems with some pages, if you open a direct link to them, they are correctly loaded, but if you switch between pages, they are loaded blank.
We have detected the issue is only produced when the page contains a javascript code with a call to the document.write code in a web content or a page fragment or page configuration
Why this problem is produced? How can I solve it?
Environment
- Any Liferay DXP 7.x version
- Any Quarterly Release
Resolution
The document.write instruction does not cause problems when the page loads normally, but it does produce errors when the page is loaded with single-page application (SPA) functionality.
The reason why document.write causes problems with single-page application (SPA) is because:
- When changing pages, this functionality loads the HTML of the new page in the background and loads the contents by modifying the DOM structure of the page in memory, making it appear to the end user to have loaded much faster.
- The downside is that after loading, this functionality has to process all the Javascript code of the updated page and force the execution of those fragments.
-
The problem is that at that point the document stream is already closed and using
document.writewhen the stream is closed implies thatdocument.open()is executed internally, which causes the document to be cleared.
You can find information about these problems caused by using document.write method in the following documentation: https://developer.mozilla.org/en-US/docs/Web/API/Document/write which explains that this method is strongly discouraged and the reason why the document is cleared.
In order to avoid the problem you are reporting, you have two options:
-
Option 1: Review the web content of your system and eliminate all uses of
document.write -
Option 2: Keep document.write and disable SPA functionality by adding the following to your portal-ext.properties:
javascript.single.page.application.enabled=true(see Disabling SPA for more information)
We recommend opting for the first option.
Additional Information
In DXP 7.4 or Quarterly releases, if you want to detect which web contents you have with the document.write line to delete, you can use the following SQL:
SELECT * FROM JournalArticle WHERE (articleId, version) IN (SELECT articleId, MAX(version) AS version FROM JournalArticle WHERE id_ IN (SELECT DISTINCT storageId FROM DDMFieldAttribute WHERE largeAttributeValue LIKE '%document.write%') GROUP BY articleId);