Redirects: Allow preventing the output of the static HTML file (#7245)
This commit is contained in:
parent
a39eb51d4c
commit
d3895a2d71
7 changed files with 64 additions and 0 deletions
|
@ -738,6 +738,28 @@ export interface AstroUserConfig {
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
serverEntry?: string;
|
serverEntry?: string;
|
||||||
|
/**
|
||||||
|
* @docs
|
||||||
|
* @name build.redirects
|
||||||
|
* @type {boolean}
|
||||||
|
* @default `true`
|
||||||
|
* @description
|
||||||
|
* Specifies whether redirects will be output to HTML during the build.
|
||||||
|
* This option only applies to `output: 'static'` mode; in SSR redirects
|
||||||
|
* are treated the same as all responses.
|
||||||
|
*
|
||||||
|
* This option is mostly meant to be used by adapters that have special
|
||||||
|
* configuration files for redirects and do not need/want HTML based redirects.
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* {
|
||||||
|
* build: {
|
||||||
|
* redirects: false
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
redirects?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -564,6 +564,10 @@ async function generatePath(
|
||||||
|
|
||||||
switch(true) {
|
switch(true) {
|
||||||
case (response.status >= 300 && response.status < 400): {
|
case (response.status >= 300 && response.status < 400): {
|
||||||
|
// If redirects is set to false, don't output the HTML
|
||||||
|
if(!opts.settings.config.build.redirects) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const location = getRedirectLocationOrThrow(response.headers);
|
const location = getRedirectLocationOrThrow(response.headers);
|
||||||
body = `<!doctype html>
|
body = `<!doctype html>
|
||||||
<title>Redirecting to: ${location}</title>
|
<title>Redirecting to: ${location}</title>
|
||||||
|
|
|
@ -22,6 +22,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
|
||||||
server: './dist/server/',
|
server: './dist/server/',
|
||||||
assets: '_astro',
|
assets: '_astro',
|
||||||
serverEntry: 'entry.mjs',
|
serverEntry: 'entry.mjs',
|
||||||
|
redirects: true,
|
||||||
},
|
},
|
||||||
compressHTML: false,
|
compressHTML: false,
|
||||||
server: {
|
server: {
|
||||||
|
@ -116,6 +117,7 @@ export const AstroConfigSchema = z.object({
|
||||||
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
||||||
assetsPrefix: z.string().optional(),
|
assetsPrefix: z.string().optional(),
|
||||||
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
||||||
|
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
||||||
})
|
})
|
||||||
.optional()
|
.optional()
|
||||||
.default({}),
|
.default({}),
|
||||||
|
@ -279,6 +281,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
|
||||||
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
||||||
assetsPrefix: z.string().optional(),
|
assetsPrefix: z.string().optional(),
|
||||||
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
||||||
|
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
||||||
})
|
})
|
||||||
.optional()
|
.optional()
|
||||||
.default({}),
|
.default({}),
|
||||||
|
|
|
@ -119,4 +119,29 @@ describe('Astro.redirect', () => {
|
||||||
expect(html).to.include('url=/');
|
expect(html).to.include('url=/');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('config.build.redirects = false', () => {
|
||||||
|
before(async () => {
|
||||||
|
process.env.STATIC_MODE = true;
|
||||||
|
fixture = await loadFixture({
|
||||||
|
root: './fixtures/ssr-redirect/',
|
||||||
|
output: 'static',
|
||||||
|
redirects: {
|
||||||
|
'/one': '/'
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
redirects: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await fixture.build();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Does not output redirect HTML', async () => {
|
||||||
|
let oneHtml = undefined;
|
||||||
|
try {
|
||||||
|
oneHtml = await fixture.readFile('/one/index.html');
|
||||||
|
} catch {}
|
||||||
|
expect(oneHtml).be.an('undefined');
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
|
@ -51,6 +51,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
|
||||||
client: new URL(`.${config.base}`, config.outDir),
|
client: new URL(`.${config.base}`, config.outDir),
|
||||||
server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir),
|
server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir),
|
||||||
serverEntry: '_worker.mjs',
|
serverEntry: '_worker.mjs',
|
||||||
|
redirects: false,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -7,6 +7,14 @@ export function netlifyStatic(): AstroIntegration {
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/netlify',
|
name: '@astrojs/netlify',
|
||||||
hooks: {
|
hooks: {
|
||||||
|
'astro:config:setup': ({ updateConfig }) => {
|
||||||
|
updateConfig({
|
||||||
|
build: {
|
||||||
|
// Do not output HTML redirects because we are building a `_redirects` file.
|
||||||
|
redirects: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
'astro:config:done': ({ config }) => {
|
'astro:config:done': ({ config }) => {
|
||||||
_config = config;
|
_config = config;
|
||||||
},
|
},
|
||||||
|
|
|
@ -43,6 +43,7 @@ export default function vercelStatic({
|
||||||
outDir,
|
outDir,
|
||||||
build: {
|
build: {
|
||||||
format: 'directory',
|
format: 'directory',
|
||||||
|
redirects: false,
|
||||||
},
|
},
|
||||||
vite: {
|
vite: {
|
||||||
define: viteDefine,
|
define: viteDefine,
|
||||||
|
|
Loading…
Add table
Reference in a new issue