@@ -7,13 +7,27 @@ import esbuild from 'esbuild';
7
7
* @typedef {import('esbuild').BuildOptions } BuildOptions
8
8
*/
9
9
10
+ const ssrFunctionRoute = '/api/__render' ;
11
+
10
12
/** @type {import('.') } */
11
13
export default function ( { debug = false } = { } ) {
12
14
return {
13
15
name : 'adapter-azure-swa' ,
14
16
15
- // implementation based on the vercel adapter
16
17
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
+
17
31
const tmp = builder . getBuildDirectory ( 'azure-tmp' ) ;
18
32
const publish = 'build' ;
19
33
const staticDir = join ( publish , 'static' ) ;
@@ -26,10 +40,27 @@ export default function ({ debug = false } = {}) {
26
40
builder . rimraf ( apiDir ) ;
27
41
28
42
builder . log . minor ( 'Prerendering static pages...' ) ;
29
- await builder . prerender ( {
43
+ const prerendered = await builder . prerender ( {
30
44
dest : staticDir
31
45
} ) ;
32
46
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
+
33
64
const files = fileURLToPath ( new URL ( './files' , import . meta. url ) ) ;
34
65
35
66
builder . log . minor ( 'Generating serverless function...' ) ;
@@ -52,10 +83,7 @@ export default function ({ debug = false } = {}) {
52
83
} ) } ;\n`
53
84
) ;
54
85
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 ) ) ;
59
87
60
88
builder . copy ( join ( files , 'api' ) , apiDir ) ;
61
89
0 commit comments