Skip to content

Commit 1a089e1

Browse files
committed
feat: allow index.html to be dynamic
1 parent 681e351 commit 1a089e1

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

files/staticwebapp.config.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ import esbuild from 'esbuild';
77
* @typedef {import('esbuild').BuildOptions} BuildOptions
88
*/
99

10+
const ssrFunctionRoute = '/api/__render';
11+
1012
/** @type {import('.')} */
1113
export default function ({ debug = false } = {}) {
1214
return {
1315
name: 'adapter-azure-swa',
1416

15-
// implementation based on the vercel adapter
1617
async adapt(builder) {
18+
const swaConfig = {
19+
routes: [
20+
{
21+
route: '*',
22+
methods: ['POST', 'PUT', 'DELETE'],
23+
rewrite: ssrFunctionRoute
24+
}
25+
],
26+
navigationFallback: {
27+
rewrite: ssrFunctionRoute
28+
}
29+
};
30+
1731
const tmp = builder.getBuildDirectory('azure-tmp');
1832
const publish = 'build';
1933
const staticDir = join(publish, 'static');
@@ -26,10 +40,27 @@ export default function ({ debug = false } = {}) {
2640
builder.rimraf(apiDir);
2741

2842
builder.log.minor('Prerendering static pages...');
29-
await builder.prerender({
43+
const prerendered = await builder.prerender({
3044
dest: staticDir
3145
});
3246

47+
if (!prerendered.paths.includes('/')) {
48+
// Azure SWA requires an index.html to be present
49+
// If the root was not pre-rendered, add a placeholder index.html
50+
// Route all requests for the index to the SSR function
51+
writeFileSync(`${staticDir}/index.html`, '');
52+
swaConfig.routes.push(
53+
{
54+
route: '/index.html',
55+
rewrite: ssrFunctionRoute
56+
},
57+
{
58+
route: '/',
59+
rewrite: ssrFunctionRoute
60+
}
61+
);
62+
}
63+
3364
const files = fileURLToPath(new URL('./files', import.meta.url));
3465

3566
builder.log.minor('Generating serverless function...');
@@ -52,10 +83,7 @@ export default function ({ debug = false } = {}) {
5283
})};\n`
5384
);
5485

55-
builder.copy(
56-
join(files, 'staticwebapp.config.json'),
57-
join(publish, 'staticwebapp.config.json')
58-
);
86+
writeFileSync(`${publish}/staticwebapp.config.json`, JSON.stringify(swaConfig));
5987

6088
builder.copy(join(files, 'api'), apiDir);
6189

0 commit comments

Comments
 (0)