-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Imports in MDX pages go to the main bundle #23345
Comments
Just in case it makes any difference, all the MDX files are located in |
The "best" workaround for now is to wrap imported components with Yes, it is known issue and there is work being done on this, but it's pretty far way from doing it nicely (for now it's discovery, I had proof of concept working but it was very limited). So for steps we need in gatsby core to make this happen:
|
Thanks a lot for a super quick response. What workaround would you suggest for imported JSON? Should I move the page out of 'src/pages', load the JSON in 'gatsby-node.js' instead of importing in '.mdx', and call 'createPage()' with the loaded JSON? |
Ah, I didn't notice that you import JSON there. I don't know to be honest how to best approach this yet other than "hiding" this in extra component: -import someData from './some-data.json';
-import SomeComponent from '../../components/some-component';
+import SomeWhatRedundantComponentThatHidesImportsAndMakeThemLazy from './lazy-thing' and then import Loadable from 'react-loadable';
const LoadableComponent = Loadable({
loader: {
Component: () => import('../../components/some-component'),
Data: () => import('./some-data.json'),
},
render(loaded, props) {
let Component = loaded.Component.default
let data = loaded.Data
return <Component someProp={data} />
}
})
export default LoadableComponent (that's using Multiple resources case for |
Wouldn't it make the page unfriendly to search engines? Would my idea of gatsby-node.js hack work? 🤔 |
Oh, I found out we should be able to do this directly in .md(x) file using similar technique as described in https://johno.com/react-hooks-in-mdx/ (so you wouldn't need separate wrapper files for it, and just use |
Potentially yes, but crawlers are pretty good at running javascript code nowadays. The |
Thanks a lot for the suggestions. Let me give them tries and report back. Should we turn this issue into a bug report or enhancement request? I can't find an issue filed for this yet. |
I worked around this problem successfully by using import loadable from '@loadable/component';
export const ArticlesProvider = loadable.lib(() => import('./articles.json'));
export const ArticleList = loadable(() => import('../../components/article-list'));
# Articles and slides
<ArticlesProvider>
{({ default: articles }) => <ArticleList dataSource={articles} />}
</ArticlesProvider> It was slightly easier to load multiple modules using |
Another workaround that works when you want to load only a JSON file into an MDX page ( // Loads the '.json' file next to the page into 'pageContext.companion'.
// Note that this will cause some GraphQL schema warnings while building,
// but it seems safe to ignore.
exports.onCreatePage = ({ page }) => {
const componentPath = page.componentPath;
if (!componentPath) {
return;
}
const ext = path.extname(componentPath);
const jsonPath = `${componentPath.substring(
0,
componentPath.length - ext.length,
)}.json`;
if (fs.existsSync(jsonPath)) {
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
// eslint-disable-next-line no-param-reassign
page.context.companion = require(jsonPath);
/* eslint-enable import/no-dynamic-require */
/* eslint-enable global-require */
}
}; For example, in <ArticleList dataSource={props.pageContext.companion} />; |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Since the original question was answered, I'll close this issue. |
Has anything changed on this since this issue was raised last year? I'm having the same problem. Large site, MDX, wide use of heavy MDX imports, all included in the main app bundle. Seriously hurting my performance. Is |
same, any updates here? |
Summary
Any imports I put into an MDX page become a part of the main bundle (
app-*.js
). For example, the following JSON file and React component in an MDX file will be added toapp-*.js
:I expect such imports to be tied to each page rather than to the main bundle, for optimal use of network traffic. This can't scale as the number of pages increases.
This comment by @pieh implies this is a known issue. Is it correct? If so, what's the best way to work around this issue?
Relevant information
N/A - I think summary explains the problem. Let me add some if requested.
Environment (if relevant)
File contents (if changed)
gatsby-config.js
: N/Apackage.json
: N/Agatsby-node.js
: N/Agatsby-browser.js
: N/Agatsby-ssr.js
: N/AThe text was updated successfully, but these errors were encountered: