Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
fix(initClient): await moving helmet scripts to prevent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
10xLaCroixDrinker committed Jul 20, 2021
1 parent b3eece0 commit c0e17b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/client/initClient.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function initClient() {
try {
// eslint-disable-next-line no-underscore-dangle
setModuleMap(global.__CLIENT_HOLOCRON_MODULE_MAP__);
moveHelmetScripts();
const helmetScriptsMoved = moveHelmetScripts();

const store = initializeClientStore();
const history = browserHistory;
Expand Down Expand Up @@ -69,6 +69,9 @@ export default async function initClient() {

const render = renderMode === 'render' ? ReactDOM.render : ReactDOM.hydrate;

// Don't render until react-helmet scripts have been moved to prevent race condition
await helmetScriptsMoved;

render(
<App />,
document.getElementById('root')
Expand Down
6 changes: 5 additions & 1 deletion src/client/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ export function loadPrerenderScripts(initialState) {
}

export function moveHelmetScripts() {
let resolve;
const promise = new Promise((res) => { resolve = res; });
document.addEventListener('DOMContentLoaded', () => {
const helmetScripts = [...document.querySelectorAll('script[data-react-helmet]')];
const helmetScripts = [...document.body.querySelectorAll('script[data-react-helmet]')];
helmetScripts.forEach((script) => document.body.removeChild(script));
helmetScripts.forEach((script) => document.head.appendChild(script));
resolve();
});
return promise;
}

export function loadServiceWorker({ dispatch, config }) {
Expand Down

0 comments on commit c0e17b1

Please sign in to comment.