Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit a4e43b0

Browse files
committed
Add support for redirects file
1 parent c15e143 commit a4e43b0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/html-plugin.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ const formatUrl = (url: string) =>
4646
const printLink = (type: 'style' | 'script') => (link: string) =>
4747
` Link: </${link}>; rel=preload; as=${type}`
4848

49+
const printRedirectsLine = ([url, path]: [string, string]) => {
50+
const input = formatUrl(url)
51+
const output = '/'
52+
if (input === output) return
53+
return `${input} ${output} 200`
54+
}
55+
56+
const redirectsTemplate = ({
57+
routes,
58+
bundle,
59+
}: {
60+
routes: Routes
61+
bundle: OutputBundle
62+
}) =>
63+
Object.entries(routes)
64+
.map(printRedirectsLine)
65+
.filter((r): r is string => r !== undefined)
66+
.join('\n')
67+
4968
const headersTemplate = ({
5069
routes,
5170
entryStyles,
@@ -124,6 +143,7 @@ in the options for the html plugin`,
124143
}
125144
const htmlPath = join(outputDir, 'index.html')
126145
const headersPath = join(outputDir, '_headers')
146+
const redirectsPath = join(outputDir, '_redirects')
127147
if (!(await exists(outputDir))) {
128148
await mkdir(outputDir)
129149
}
@@ -138,6 +158,13 @@ in the options for the html plugin`,
138158
bundle,
139159
}),
140160
),
161+
writeFile(
162+
redirectsPath,
163+
redirectsTemplate({
164+
routes,
165+
bundle,
166+
}),
167+
),
141168
])
142169
},
143170
}

0 commit comments

Comments
 (0)