Hi
When running yarn rw build web
our project fails to build because an ES module import is done in an unrelated layout not used by the pages we are trying to prerender. I found this related topic on prerendering, but their problem was importing ES modules directly in prerendered pages.
Our repository and code look like this (if this is a bug, I can upload a minimal reproduction to help solve the issue).
- api/
- packages/
- shared/
- web/
// routes.tsx
import { Set, Router, Route } from '@redwoodjs/router'
import DashboardLayout from 'src/layouts/DashboardLayout'
import Footer from 'src/layouts/Footer'
import Header from 'src/layouts/Header'
const Routes = () => {
return (
<Router>
<Set wrap={[Header, Footer]}>
<Route path="/" page={LandingPage} name="landingPage" prerender />
<Route path="/about" page={AboutPage} name="aboutPage" prerender />
<Route path="/contact" page={ContactPage} name="contactPage" prerender />
</Set>
<Set wrap={[DashboardLayout]}>
<Route path="/team/{slug}" page={DashboardPage} name="dashboardPage" />
{/* ... more */}
</Set>
</Router>
)
}
packages/shared
contains utils functions we use in web
and api
. This has been working for a good while until we started importing the package in DashboardLayout
and DashboardPage
which causes the following error when prerendering:
✔ Generating Prisma Client...
✔ Building Web...
Creating 200.html...
Starting prerendering...
✖ Failed to render "repo\web\src\pages\AboutPage\AboutPage.tsx"
◼ Prerendering /contact -> web/dist/contact.html
You can use `yarn rw prerender --dry-run` to debug
---------- Error rendering path "/about" ----------
Error [ERR_REQUIRE_ESM]: require() of ES Module repo\packages\shared\dist\src\index.js from repo\web\src\layouts\DashboardLayout\DashboardLayout.tsx not supported.
Instead change the require of index.js in repo\web\src\layouts\DashboardLayout\DashboardLayout.tsx to a dynamic import() which is available in all CommonJS modules.
at Object.newLoader [as .tsx] (repo\node_modules\pirates\lib\index.js:121:7)
at Object.<anonymous> (repo\web\src\layouts\DashboardLayout\DashboardLayout.tsx:10:58)
at Module._compile (repo\node_modules\pirates\lib\index.js:117:24)
at Object.newLoader [as .tsx] (repo\node_modules\pirates\lib\index.js:121:7)
at Object.<anonymous> (repo\web\src\Routes.tsx:18:57)
at Module._compile (repo\node_modules\pirates\lib\index.js:117:24)
at Object.newLoader [as .tsx] (repo\node_modules\pirates\lib\index.js:121:7)
at Object.<anonymous> (repo\web\src\App.tsx:12:38)
at Module._compile (repo\node_modules\pirates\lib\index.js:117:24)
at Object.newLoader [as .tsx] (repo\node_modules\pirates\lib\index.js:121:7)
at runPrerender (repo\node_modules\@redwoodjs\prerender\dist\runPrerender.js:257:7)
at async _Task.task [as taskFn] (repo\node_modules\@redwoodjs\cli\dist\commands\prerenderHandler.js:158:35)
at async _Task.run (repo\node_modules\listr2\dist\index.cjs:2049:11)
Reading the stack trace it seems to occur because Routes.tsx
imports DashboardLayout.tsx
and so on. I have tried to use some of the tricks mentioned in Prerender | RedwoodJS Docs to prevent the import, but it wasn’t possible.
Any help on the issue is greatly appreciated.