-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic page loading failed Error: Trying to replace an element that wasn't mounted! #987
Comments
Since this seems to be an SSR related issue, can you post some code on how you integrated our SSR API with the boilerplate? Cheers cc @geelen |
Hi Phil, thanks for a quick response. About 3 months ago this PR was forked to bootstrap the project. I've followed instructions from the docs. The relevant code part (capturing of styles) below: // 1st render phase - triggers the sagas
renderToString(
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
);
// send signal to sagas that we're done
store.dispatch(END);
// wait for all tasks to finish
await sagasDone();
// capture the state after the first render
const state = store.getState().toJS();
const sheet = new ServerStyleSheet();
// 2nd render phase - the sagas triggered in the first phase are resolved by now
const appMarkup = renderToString(
sheet.collectStyles(
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
)
);
// capture the generated css
const css = sheet.getStyleElement();
const doc = renderToStaticMarkup(
<HtmlDocument
appMarkup={appMarkup}
state={state}
head={Helmet.rewind()}
assets={assets}
css={css}
webpackDllNames={webpackDllNames}
/>
);
return `<!DOCTYPE html>\n${doc}`; |
hm, I suspect that the two render phases are responsible for this trouble. Can you try this: import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
// 1st render phase - triggers the sagas
renderToString(
<StyleSheetManager sheet={new ServerStyleSheet().instance}>
);
// ...
// 2nd render phase
const sheet = new ServerStyleSheet();
const appMarkup = renderToString(
sheet.collectStyles(
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
)
);
// ...
const css = sheet.getStyleElement(); The problem seems to be that in the 1st phase you're rendering loads of Styled Components without them being captured with the server stylesheet. We can't reuse the same one for two rendering cycles, but we can try to use two separate ones, I believe |
Tried this: import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
// ...
// 1st render phase - triggers the sagas
renderToString(
<StyleSheetManager sheet={new ServerStyleSheet().instance}>
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
</StyleSheetManager>
);
// ...
// 2nd render phase - the sagas triggered in the first phase are resolved by now
const sheet = new ServerStyleSheet();
const appMarkup = renderToString(
sheet.collectStyles(
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
)
);
// capture the generated css
const css = sheet.getStyleElement(); and this import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
// ...
const sheet = new ServerStyleSheet();
// 1st render phase - triggers the sagas
renderToString(
<StyleSheetManager sheet={sheet.instance}>
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
</StyleSheetManager>
);
// ...
// 2nd render phase - the sagas triggered in the first phase are resolved by now
const appMarkup = renderToString(
sheet.collectStyles(
<AppRoot store={store}>
<RouterContext {...renderProps} />
</AppRoot>
)
);
// capture the generated css
const css = sheet.getStyleElement(); Both result in the same error. As for loading a lot of components during the first render cycle - I think we are loading the same (or lower) amount of components, because when sagas are done we have additional components to display with new data. |
Is there anything special about these components in the way they're used together with the state? |
After reading through the code, I noticed that the error occurs when a style tag is not mounted in the head anymore. Which sounds weird. I'm not familiar with the rehydration yet, so @geelen might have to jump in. Are you sure that the style tags are injected into the head statically and stay there? https://github.com/styled-components/styled-components/blob/master/src/models/BrowserStyleSheet.js |
There's nothing special about those components (except from the fact that they are not used in the page that gets this error). An example of a particular component above: import styled from 'styled-components';
const Quote = styled.p`
font-weight: 700;
font-family: Abhaya Libre,serif;
font-size: 30px;
line-height: 1.6;
border-left: 5px solid #9df6df;
text-align: left;
padding-left: 10px;
&::after {
content: ' ”';
}
&::before {
content: '“ ';
}
`;
export default Quote; This particular component is created when we I'm 100% sure that the styles are injected into the head. Here's how they look: <style type="text/css" data-styled-components="rPGQc kGbJUQ bQHhuX eEjGQg gCQUri euQgqQ jsGwgt czKzNZ fXGJEd esYmnI lgKJiB dawqLu gUBkcu bgATzB ddpDRo daFMBo iEzPRN jPjSWh cMdELy gIcplz dHFWjw MUOSJ hXQcQI cIVxBE bGQeLV jSyOU jVdDWo TaYKd dsYfnr hTeuSa jQwvVs" data-styled-components-is-local="true">/* sc-component-id: StyledHero__StyledHero-oeql2j-0 */
.StyledHero__StyledHero-oeql2j-0 {}
.czKzNZ{display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;-webkit-box-pack: center;-webkit-justify-content: center;-ms-flex-pack: center;justify-content: center;-webkit-align-items: center;-webkit-box-align: center;-ms-flex-align: center;align-items: center;-webkit-flex-direction: column;-ms-flex-direction: column;flex-direction: column;padding: 160px 20px 0;width: 100%; ;}
/* sc-component-id: HeroSubtitle__HeroSubtitle-urumro-0 */
.HeroSubtitle__HeroSubtitle-urumro-0 {}
... |
I know this is far from ideal, but would it be possible to fail silently? Because when this error is thrown, the execution of the code stops and the page "breaks", which is undesired. |
@DeividasK I'm not sure about that either, because it's a critical error in the rehydration. Again, Glen will probably be able to shine a light on this |
@geelen Hey Glen, just wanted to bump this up. Any ideas / experiments that I could do to try and fix this would be really helpful. |
He guys, any updates on this? |
@DeividasK I can probably take a quick look and try out some things to solve this. Do you have a reproduction repo somewhere? |
Hey Phil, not really. The main problem is that it is hard to precisely reproduce. The only way I am able to reproduce it now is in the main project repo. It disappears if I remove server side rendering. Removing code splitting does not help. |
@philpl turns out that the issue was with duplicated styled-components modules. One of the dependencies for our npm package was externals: {
react: 'react',
'react-dom': 'react-dom',
'styled-components': 'styled-components',
}, the issues have disappeared. Simply removing it from dependencies did not help as it was necessary to specify it together with other external libraries. I closed this issue, but I think it's worth having a discussion if styled-components should somehow detect and prevent this from happening. |
For anyone who googled and found this. I had a similar issue using I was doing something very similar to @DeividasK const sheet = new ServerStyleSheet();
const appHTML = renderToStaticMarkup(
<StyleSheetManager sheet={sheet.instance}>
<Provider store={store} key="provider">
<ReduxAsyncConnect {...renderProps} />
</Provider>
</StyleSheetManager>
);
// capture the generated css
const css = sheet.getStyleElement();
const doc = renderToStaticMarkup(
<HtmlDocument
appMarkup={appHTML}
css={css}
/>
);
res.send('<!DOCTYPE html>' + doc); But on the client after the JS loads I would get an error I was able to get rid of the error on the client by changing
and in my <head>
{css}
</head> to <head>
<style dangerouslySetInnerHTML={{ __html: css}} />
</head> |
Occured when server side rendering any page other than index Fix found at: styled-components/styled-components#987
Replicating B’s commit on Strata site: callumflack/vjray-strata@103f62d70dda3f447fd85 b831556be4b518c4bc9 Occured when server side rendering any page other than index Fix found at: styled-components/styled-components#987
@xiaolin I get the same error |
@ilanus This issue is extremely old! Please refrain from commenting in older issues that have been resolved Check the docs on how to set up SSR: https://www.styled-components.com/docs/advanced#server-side-rendering
|
See the solution (and issue) here: styled-components/styled-components#987 (comment)
This can be caused by multiple with npm use
|
This is the relevant issue: styled-components/styled-components#987
For more context on this issue see: styled-components/styled-components#987
Version
styled-components
: 2.1.1babel-plugin-styled-components
: 1.1.7styled-components
.Error stack trace
Reproduction
I cannot reliably reproduce the error on webpackbin. Things I can reproduce:
styled-components
v1. It started occurring once we switched to v2 (without any other changes to the setup).What I've tried
babel-plugin-styled-components
optionsssr
on/offpreprocess
on/offAny suggestion that could point to the possible culprit / solution would be greatly appreciated.
The text was updated successfully, but these errors were encountered: