Skip to content

Commit

Permalink
Fix 'Failed to resolve import' crash during ssr (#1496)
Browse files Browse the repository at this point in the history
* catch import errors in lazyRoute in dev

* provide placeholder component when error occurs

* fallback to empty route

* Create mighty-teachers-guess.md

---------

Co-authored-by: Ryan Carniato <[email protected]>
  • Loading branch information
Brendonovich and ryansolid authored May 23, 2024
1 parent d442a83 commit 3ec4138
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-teachers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Fix 'Failed to resolve import' crash during ssr
11 changes: 8 additions & 3 deletions packages/start/src/router/lazyRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ export default function lazyRoute<T>(component: any, clientManifest: any, server
if (import.meta.env.DEV) {
let manifest = import.meta.env.SSR ? serverManifest : clientManifest;

const mod = await manifest.inputs[component.src].import();
// import() throws if a module doesn't exist, which includes any
// modules loaded by the route itself, so it's important we catch here
const mod = await manifest.inputs[component.src].import().catch(() => null);
// fallback to an empty component as any errors will surface in the vite overlay
if(!mod) return { default: () => [] };

if (!mod[exported]) console.error(`Module ${component.src} does not export ${exported}`);
const Component = mod[exported];
let assets = await clientManifest.inputs?.[component.src].assets();
const Component = mod[exported]
let assets = await clientManifest.inputs?.[component.src]?.assets();
const styles = assets.filter((asset: Asset) => asset.tag === "style");

if (typeof window !== "undefined" && import.meta.hot) {
Expand Down
2 changes: 1 addition & 1 deletion packages/start/src/server/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
// @ts-ignore
const nonce = context.nonce;

let assets: Asset[];
let assets: Asset[] = [];
Promise.resolve().then(async () => {
let assetPromises: Promise<Asset[]>[] = [];
// @ts-ignore
Expand Down

0 comments on commit 3ec4138

Please sign in to comment.