Skip to content

Commit 70702ff

Browse files
biltongzageoffrich
andauthored
feat: add customStaticWebAppConfig option (#28)
* allow extended SWA config * handle overriding routes * rename ExtendStaticWebAppConfig to CustomStaticWebAppConfig * default custom config to empty object * Add note about extending the generated SWA config * Update docs, refactor, and format * Tweak error messaging Co-authored-by: Geoff Rich <[email protected]>
1 parent 66e4cc4 commit 70702ff

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,47 @@ To run the CLI, install `@azure/static-web-apps-cli` and the [Azure Functions Co
6565
}
6666
}
6767
```
68+
69+
## Options
70+
71+
### customStaticWebAppConfig
72+
73+
An object containing additional Azure SWA [configuration options](https://docs.microsoft.com/en-us/azure/static-web-apps/configuration). This will be merged with the `staticwebapp.config.json` generated by the adapter.
74+
75+
Attempting to override the default catch-all route (`route: '*'`) or the `navigationFallback` options will throw an error, since they are critical for server-side rendering.
76+
77+
**Note:** customizing this config (especially `routes`) has the potential to break how SvelteKit handles the request. Make sure to test any modifications thoroughly.
78+
79+
```js
80+
import azure from 'svelte-adapter-azure-swa';
81+
82+
export default {
83+
kit: {
84+
...
85+
adapter: azure({
86+
customStaticWebAppConfig: {
87+
routes: [
88+
{
89+
route: '/login',
90+
allowedRoles: ['admin']
91+
}
92+
],
93+
globalHeaders: {
94+
'X-Content-Type-Options': 'nosniff',
95+
'X-Frame-Options': 'DENY',
96+
'Content-Security-Policy': "default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'",
97+
},
98+
mimeTypes: {
99+
'.json': 'text/json'
100+
},
101+
responseOverrides: {
102+
'401': {
103+
'redirect': '/login',
104+
'statusCode': 302
105+
}
106+
}
107+
}
108+
})
109+
}
110+
};
111+
```

index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { Adapter } from '@sveltejs/kit';
2+
import { CustomStaticWebAppConfig } from './types/swa';
23

3-
declare function plugin(opts?: { debug?: boolean }): Adapter;
4+
declare function plugin(opts?: {
5+
debug?: boolean;
6+
customStaticWebAppConfig?: CustomStaticWebAppConfig;
7+
}): Adapter;
48
export = plugin;

index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,38 @@ import esbuild from 'esbuild';
99

1010
const ssrFunctionRoute = '/api/__render';
1111

12+
/**
13+
* Validate the static web app configuration does not override the minimum config for the adapter to work correctly.
14+
* @param config {import('./types/swa').CustomStaticWebAppConfig}
15+
* */
16+
function validateCustomConfig(config) {
17+
if (config) {
18+
if ('navigationFallback' in config) {
19+
throw new Error('customStaticWebAppConfig cannot override navigationFallback.');
20+
}
21+
if (config.routes && config.routes.find((route) => route.route === '*')) {
22+
throw new Error(`customStaticWebAppConfig cannot override '*' route.`);
23+
}
24+
}
25+
}
26+
1227
/** @type {import('.')} */
13-
export default function ({ debug = false } = {}) {
28+
export default function ({ debug = false, customStaticWebAppConfig = {} } = {}) {
1429
return {
1530
name: 'adapter-azure-swa',
1631

1732
async adapt(builder) {
33+
validateCustomConfig(customStaticWebAppConfig);
34+
35+
if (!customStaticWebAppConfig.routes) {
36+
customStaticWebAppConfig.routes = [];
37+
}
38+
1839
/** @type {import('./types/swa').StaticWebAppConfig} */
1940
const swaConfig = {
41+
...customStaticWebAppConfig,
2042
routes: [
43+
...customStaticWebAppConfig.routes,
2144
{
2245
route: '*',
2346
methods: ['POST', 'PUT', 'DELETE'],

types/swa.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
export interface StaticWebAppConfig {
33
routes?: Route[];
44
navigationFallback?: NavigationFallback;
5+
globalHeaders?: Record<string, string>;
6+
responseOverrides?: Record<OverridableResponseCodes, ResponseOverride>;
7+
mimeTypes?: Record<string, string>;
58
}
69

10+
export type CustomStaticWebAppConfig = Omit<StaticWebAppConfig, 'navigationFallback'>;
11+
712
export interface Route {
813
route: string;
914
methods?: HttpMethod[];
@@ -29,3 +34,11 @@ export type HttpMethod =
2934
| 'OPTIONS'
3035
| 'TRACE'
3136
| 'PATCH';
37+
38+
export interface ResponseOverride {
39+
rewrite?: string;
40+
statusCode?: number;
41+
redirect?: string;
42+
}
43+
44+
export type OverridableResponseCodes = '400' | '401' | '403' | '404';

0 commit comments

Comments
 (0)