React preloading components with Lazy + Suspense
up vote
2
down vote
favorite
I'm currently using React 16 with Suspense and Lazy to code-split my codebase. Although I would like to preload components.
In my example below I got two routes. Is there a way to preload Demo
as soon Prime
did mount? I've tried to create another dynamic import in the componentDidMount
of the Prime
page, but React.lazy
doesn't seem to get that that's the same file as the dynamic import below.
import React, lazy, Suspense from 'react';
import Switch, Route, withRouter from 'react-router-dom';
import GlobalStyle from 'styles';
import Loading from 'common/Loading';
const Prime = lazy(() => import(/* webpackChunkName: "Prime" */'modules/Prime'));
const Demo = lazy(() => import(/* webpackChunkName: "Demo" */'modules/Demo'));
const App = () => (
<main>
<GlobalStyle />
<Suspense fallback=<Loading>Loading...</Loading>>
<Switch>
<Route path="/" component=Prime exact />
<Route path="/demo" component=Demo />
</Switch>
</Suspense>
</main>
);
export default withRouter(App);
So I've tried different approaches, for example with and without webpackChunkName
and different ways of importing the other component in componentDidMount
, as seen below. The first two approaches of importing the file in componentDidMount
resulted in a Webpack error shown at the bottom of the image below. Only the third proceeded, but made the file 2.[hash].js
in the image, only load after the page was visited, and not on componentDidMount
What am I missing here?
Code of modules/Demo.jsx
:
import React from 'react';
import LogoIcon from 'vectors/logo.svg';
import PageLink from 'common/PageLink';
import Anchor from 'common/Anchor';
import CenteredSection from 'common/CenteredSection';
const Demo = () => (
<CenteredSection variant="green">
<LogoIcon />
<PageLink to="/" variant="green">Go to home page</PageLink>
</CenteredSection>
);
export default Demo;
reactjs preload dynamic-import react-16
add a comment |
up vote
2
down vote
favorite
I'm currently using React 16 with Suspense and Lazy to code-split my codebase. Although I would like to preload components.
In my example below I got two routes. Is there a way to preload Demo
as soon Prime
did mount? I've tried to create another dynamic import in the componentDidMount
of the Prime
page, but React.lazy
doesn't seem to get that that's the same file as the dynamic import below.
import React, lazy, Suspense from 'react';
import Switch, Route, withRouter from 'react-router-dom';
import GlobalStyle from 'styles';
import Loading from 'common/Loading';
const Prime = lazy(() => import(/* webpackChunkName: "Prime" */'modules/Prime'));
const Demo = lazy(() => import(/* webpackChunkName: "Demo" */'modules/Demo'));
const App = () => (
<main>
<GlobalStyle />
<Suspense fallback=<Loading>Loading...</Loading>>
<Switch>
<Route path="/" component=Prime exact />
<Route path="/demo" component=Demo />
</Switch>
</Suspense>
</main>
);
export default withRouter(App);
So I've tried different approaches, for example with and without webpackChunkName
and different ways of importing the other component in componentDidMount
, as seen below. The first two approaches of importing the file in componentDidMount
resulted in a Webpack error shown at the bottom of the image below. Only the third proceeded, but made the file 2.[hash].js
in the image, only load after the page was visited, and not on componentDidMount
What am I missing here?
Code of modules/Demo.jsx
:
import React from 'react';
import LogoIcon from 'vectors/logo.svg';
import PageLink from 'common/PageLink';
import Anchor from 'common/Anchor';
import CenteredSection from 'common/CenteredSection';
const Demo = () => (
<CenteredSection variant="green">
<LogoIcon />
<PageLink to="/" variant="green">Go to home page</PageLink>
</CenteredSection>
);
export default Demo;
reactjs preload dynamic-import react-16
Usinglazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code formodules/Demo
since it seems that something is causing a cyclic dependency.
– Ryan C
Nov 11 at 21:05
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
The code structure is a little different than I was expecting. I was expecting to seemodules/Demo.jsx
andmodules/Prime.jsx
. I'm not saying that what you have is "wrong", but havingDemo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.
– Ryan C
Nov 11 at 21:55
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm currently using React 16 with Suspense and Lazy to code-split my codebase. Although I would like to preload components.
In my example below I got two routes. Is there a way to preload Demo
as soon Prime
did mount? I've tried to create another dynamic import in the componentDidMount
of the Prime
page, but React.lazy
doesn't seem to get that that's the same file as the dynamic import below.
import React, lazy, Suspense from 'react';
import Switch, Route, withRouter from 'react-router-dom';
import GlobalStyle from 'styles';
import Loading from 'common/Loading';
const Prime = lazy(() => import(/* webpackChunkName: "Prime" */'modules/Prime'));
const Demo = lazy(() => import(/* webpackChunkName: "Demo" */'modules/Demo'));
const App = () => (
<main>
<GlobalStyle />
<Suspense fallback=<Loading>Loading...</Loading>>
<Switch>
<Route path="/" component=Prime exact />
<Route path="/demo" component=Demo />
</Switch>
</Suspense>
</main>
);
export default withRouter(App);
So I've tried different approaches, for example with and without webpackChunkName
and different ways of importing the other component in componentDidMount
, as seen below. The first two approaches of importing the file in componentDidMount
resulted in a Webpack error shown at the bottom of the image below. Only the third proceeded, but made the file 2.[hash].js
in the image, only load after the page was visited, and not on componentDidMount
What am I missing here?
Code of modules/Demo.jsx
:
import React from 'react';
import LogoIcon from 'vectors/logo.svg';
import PageLink from 'common/PageLink';
import Anchor from 'common/Anchor';
import CenteredSection from 'common/CenteredSection';
const Demo = () => (
<CenteredSection variant="green">
<LogoIcon />
<PageLink to="/" variant="green">Go to home page</PageLink>
</CenteredSection>
);
export default Demo;
reactjs preload dynamic-import react-16
I'm currently using React 16 with Suspense and Lazy to code-split my codebase. Although I would like to preload components.
In my example below I got two routes. Is there a way to preload Demo
as soon Prime
did mount? I've tried to create another dynamic import in the componentDidMount
of the Prime
page, but React.lazy
doesn't seem to get that that's the same file as the dynamic import below.
import React, lazy, Suspense from 'react';
import Switch, Route, withRouter from 'react-router-dom';
import GlobalStyle from 'styles';
import Loading from 'common/Loading';
const Prime = lazy(() => import(/* webpackChunkName: "Prime" */'modules/Prime'));
const Demo = lazy(() => import(/* webpackChunkName: "Demo" */'modules/Demo'));
const App = () => (
<main>
<GlobalStyle />
<Suspense fallback=<Loading>Loading...</Loading>>
<Switch>
<Route path="/" component=Prime exact />
<Route path="/demo" component=Demo />
</Switch>
</Suspense>
</main>
);
export default withRouter(App);
So I've tried different approaches, for example with and without webpackChunkName
and different ways of importing the other component in componentDidMount
, as seen below. The first two approaches of importing the file in componentDidMount
resulted in a Webpack error shown at the bottom of the image below. Only the third proceeded, but made the file 2.[hash].js
in the image, only load after the page was visited, and not on componentDidMount
What am I missing here?
Code of modules/Demo.jsx
:
import React from 'react';
import LogoIcon from 'vectors/logo.svg';
import PageLink from 'common/PageLink';
import Anchor from 'common/Anchor';
import CenteredSection from 'common/CenteredSection';
const Demo = () => (
<CenteredSection variant="green">
<LogoIcon />
<PageLink to="/" variant="green">Go to home page</PageLink>
</CenteredSection>
);
export default Demo;
reactjs preload dynamic-import react-16
reactjs preload dynamic-import react-16
edited Nov 11 at 21:43
asked Nov 11 at 20:21
ronnyrr
396828
396828
Usinglazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code formodules/Demo
since it seems that something is causing a cyclic dependency.
– Ryan C
Nov 11 at 21:05
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
The code structure is a little different than I was expecting. I was expecting to seemodules/Demo.jsx
andmodules/Prime.jsx
. I'm not saying that what you have is "wrong", but havingDemo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.
– Ryan C
Nov 11 at 21:55
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00
add a comment |
Usinglazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code formodules/Demo
since it seems that something is causing a cyclic dependency.
– Ryan C
Nov 11 at 21:05
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
The code structure is a little different than I was expecting. I was expecting to seemodules/Demo.jsx
andmodules/Prime.jsx
. I'm not saying that what you have is "wrong", but havingDemo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.
– Ryan C
Nov 11 at 21:55
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00
Using
lazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code for modules/Demo
since it seems that something is causing a cyclic dependency.– Ryan C
Nov 11 at 21:05
Using
lazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code for modules/Demo
since it seems that something is causing a cyclic dependency.– Ryan C
Nov 11 at 21:05
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
The code structure is a little different than I was expecting. I was expecting to see
modules/Demo.jsx
and modules/Prime.jsx
. I'm not saying that what you have is "wrong", but having Demo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.– Ryan C
Nov 11 at 21:55
The code structure is a little different than I was expecting. I was expecting to see
modules/Demo.jsx
and modules/Prime.jsx
. I'm not saying that what you have is "wrong", but having Demo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.– Ryan C
Nov 11 at 21:55
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Not sure how much help this will be, but here is a code sandbox that works (Demo gets loaded by componentDidMount). It is a considerably simplified version of your code using create-react-app for the config. Perhaps you can take this as a starting point and morph it gradually closer to your app to see what causes the dynamic import to no longer work as desired.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252861%2freact-preloading-components-with-lazy-suspense%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Not sure how much help this will be, but here is a code sandbox that works (Demo gets loaded by componentDidMount). It is a considerably simplified version of your code using create-react-app for the config. Perhaps you can take this as a starting point and morph it gradually closer to your app to see what causes the dynamic import to no longer work as desired.
add a comment |
up vote
0
down vote
Not sure how much help this will be, but here is a code sandbox that works (Demo gets loaded by componentDidMount). It is a considerably simplified version of your code using create-react-app for the config. Perhaps you can take this as a starting point and morph it gradually closer to your app to see what causes the dynamic import to no longer work as desired.
add a comment |
up vote
0
down vote
up vote
0
down vote
Not sure how much help this will be, but here is a code sandbox that works (Demo gets loaded by componentDidMount). It is a considerably simplified version of your code using create-react-app for the config. Perhaps you can take this as a starting point and morph it gradually closer to your app to see what causes the dynamic import to no longer work as desired.
Not sure how much help this will be, but here is a code sandbox that works (Demo gets loaded by componentDidMount). It is a considerably simplified version of your code using create-react-app for the config. Perhaps you can take this as a starting point and morph it gradually closer to your app to see what causes the dynamic import to no longer work as desired.
answered Nov 12 at 1:03
Ryan C
723210
723210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252861%2freact-preloading-components-with-lazy-suspense%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Using
lazy
within componentDidMount of Prime won't do you any good for the preload case since it causes the component to be loaded when it is rendered. Approaches 2 and 3 seem fine at first glance, but the error you're getting for 1 and 2 makes me want to see the code formodules/Demo
since it seems that something is causing a cyclic dependency.– Ryan C
Nov 11 at 21:05
@RyanC thanks for your response. I've added the code of the Demo page. You can also view to codebase over here: github.com/JBostelaar/react-prime/tree/progressive-web-app/src I also thought the first approach couldn't work, but still I wanted to show what I've tried. 2 gives the error, 3 doesn't preload the page.
– ronnyrr
Nov 11 at 21:45
The code structure is a little different than I was expecting. I was expecting to see
modules/Demo.jsx
andmodules/Prime.jsx
. I'm not saying that what you have is "wrong", but havingDemo/index.jsx
instead just deviates from approaches that I have used that I can give more confident direction on.– Ryan C
Nov 11 at 21:55
Thanks for your input, but I’m pretty sure that the issue doesn’t come from my folder structure or naming.
– ronnyrr
Nov 11 at 22:00