0dd0ef226c
This PR fixes tree-shaking for the metadata image generation module (e.g. `opengraph-image.js` and other conventions) when the page has `runtime = 'edge'`. ## Details The first step of this fix is to change this from the loader: ```js import * as exported from "./opengraph-image.js" ``` to be necessary fields only (so the `default` export can potentially be removed): ```js import { alt, size } from "./opengraph-image.js" ``` To know which fields are exported, we need to load the module first via Webpack loader's `loadModule` API and check its `HarmonyExportSpecifierDependency` dependencies. This is the first step to make it tree-shakable. Since we have `./opengraph-image.js` used in another entry, the actual image API route `opengraph-image/route.js`: ```js import * as image from "./opengraph-image.js" ``` Webpack still treats both as the same module and generates one chunk for it. We want to "fork" it into two modules. The technique here is to add a noop resource query and make it: ```js import { alt, size } from "./opengraph-image.js?__next_metadata_image_meta__" ``` So it won't be shared in the chunk (as it's a different request), and can be concatenated inline. However that's not enough, the inlined result will still have all imports from our `opengraph-image.js`, including `import { ImageResponse } from 'next/server'`. Because we can't simply add `"sideEffects": false` in Next.js' package.json, we need a way to mark this import as side-effect free. I went through https://github.com/webpack/webpack/blob/main/lib/optimize/SideEffectsFlagPlugin.js and used the same method to mark that module with `module.factoryMeta.sideEffectFree = true`. With all these added, the page bundle will no longer contain the `ImageResponse` instance. ## Result The difference is quite amazing, for the new added test (an empty Edge runtime page with an opengrah image file) here're the before/after metrics for the `page.js` server bundle: Edge bundle size: 892kB → 500kB. Build time: 26.792s → 8.830s. |
||
---|---|---|
.cargo | ||
.config | ||
.devcontainer | ||
.github | ||
.husky | ||
.vscode | ||
bench | ||
contributing | ||
docs | ||
errors | ||
examples | ||
packages | ||
scripts | ||
test | ||
.alexignore | ||
.alexrc | ||
.eslintignore | ||
.eslintrc.json | ||
.gitattributes | ||
.gitignore | ||
.npmrc | ||
.prettierignore | ||
.prettierignore_staged | ||
.prettierrc.json | ||
.rustfmt.toml | ||
.vercel.approvers | ||
azure-pipelines.yml | ||
Cargo.lock | ||
Cargo.toml | ||
CODE_OF_CONDUCT.md | ||
contributing.md | ||
jest.config.js | ||
jest.replay.config.js | ||
lerna.json | ||
license.md | ||
lint-staged.config.js | ||
package.json | ||
plopfile.js | ||
pnpm-lock.yaml | ||
pnpm-workspace.yaml | ||
readme.md | ||
release.js | ||
run-tests.js | ||
rust-toolchain | ||
test-file.txt | ||
tsconfig-tsec.json | ||
tsconfig.json | ||
tsec-exemptions.json | ||
turbo.json | ||
UPGRADING.md | ||
vercel.json |
Next.js
Getting Started
Visit https://nextjs.org/learn to get started with Next.js.
Documentation
Visit https://nextjs.org/docs to view the full documentation.
Who is using Next.js?
Next.js is used by the world's leading companies. Check out the Next.js Showcase to learn more.
Community
The Next.js community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.
To chat with other community members you can join the Next.js Discord.
Our Code of Conduct applies to all Next.js community channels.
Contributing
Please see our contributing.md.
Good First Issues
We have a list of good first issues that contain bugs that have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.
Authors
- Tim Neutkens (@timneutkens)
- Naoyuki Kanezawa (@nkzawa)
- Guillermo Rauch (@rauchg)
- Arunoda Susiripala (@arunoda)
- Tony Kovanen (@tonykovanen)
- Dan Zajdband (@impronunciable)
Security
If you believe you have found a security vulnerability in Next.js, we encourage you to responsibly disclose this and not open a public issue. We will investigate all legitimate reports. Email security@vercel.com
to disclose any security vulnerabilities.