Skip to content

Commit 0e7960a

Browse files
itlackeygeoffrich
andauthored
fix: prevent file not found error when index not prerendered (#39)
* Fixing an issue with missing folder If there are no prerendered pages, the build would throw a file not found error since staticDir is rimraf'ed on line 70. It's an odd corner case but I did end up hitting it. * refactor: write additional files after SvelteKit Co-authored-by: Geoff Rich <[email protected]>
1 parent e1bfc42 commit 0e7960a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

index.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,6 @@ export default function ({ debug = false, customStaticWebAppConfig = {} } = {})
6868
builder.rimraf(tmp);
6969
builder.rimraf(publish);
7070
builder.rimraf(apiDir);
71-
builder.mkdirp(publish);
72-
73-
if (!builder.prerendered.paths.includes('/')) {
74-
// Azure SWA requires an index.html to be present
75-
// If the root was not pre-rendered, add a placeholder index.html
76-
// Route all requests for the index to the SSR function
77-
writeFileSync(`${staticDir}/index.html`, '');
78-
swaConfig.routes.push(
79-
{
80-
route: '/index.html',
81-
rewrite: ssrFunctionRoute
82-
},
83-
{
84-
route: '/',
85-
rewrite: ssrFunctionRoute
86-
}
87-
);
88-
}
8971

9072
const files = fileURLToPath(new URL('./files', import.meta.url));
9173

@@ -109,8 +91,6 @@ export default function ({ debug = false, customStaticWebAppConfig = {} } = {})
10991
})};\n`
11092
);
11193

112-
writeFileSync(`${publish}/staticwebapp.config.json`, JSON.stringify(swaConfig));
113-
11494
builder.copy(join(files, 'api'), apiDir);
11595

11696
/** @type {BuildOptions} */
@@ -128,6 +108,25 @@ export default function ({ debug = false, customStaticWebAppConfig = {} } = {})
128108
builder.writeStatic(staticDir);
129109
builder.writeClient(staticDir);
130110
builder.writePrerendered(staticDir);
111+
112+
if (!builder.prerendered.paths.includes('/')) {
113+
// Azure SWA requires an index.html to be present
114+
// If the root was not pre-rendered, add a placeholder index.html
115+
// Route all requests for the index to the SSR function
116+
writeFileSync(`${staticDir}/index.html`, '');
117+
swaConfig.routes.push(
118+
{
119+
route: '/index.html',
120+
rewrite: ssrFunctionRoute
121+
},
122+
{
123+
route: '/',
124+
rewrite: ssrFunctionRoute
125+
}
126+
);
127+
}
128+
129+
writeFileSync(`${publish}/staticwebapp.config.json`, JSON.stringify(swaConfig));
131130
}
132131
};
133132
}

0 commit comments

Comments
 (0)