Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirects: Allow preventing the output of the static HTML file #7245

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,28 @@ export interface AstroUserConfig {
* ```
*/
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;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ async function generatePath(

switch(true) {
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);
body = `<!doctype html>
<title>Redirecting to: ${location}</title>
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
server: './dist/server/',
assets: '_astro',
serverEntry: 'entry.mjs',
redirects: true,
},
compressHTML: false,
server: {
Expand Down Expand Up @@ -116,6 +117,7 @@ export const AstroConfigSchema = z.object({
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z.string().optional(),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
})
.optional()
.default({}),
Expand Down Expand Up @@ -279,6 +281,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z.string().optional(),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
})
.optional()
.default({}),
Expand Down
25 changes: 25 additions & 0 deletions packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,29 @@ describe('Astro.redirect', () => {
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');
})
})
});
1 change: 1 addition & 0 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
client: new URL(`.${config.base}`, config.outDir),
server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir),
serverEntry: '_worker.mjs',
redirects: false,
},
});
},
Expand Down
8 changes: 8 additions & 0 deletions packages/integrations/netlify/src/integration-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export function netlifyStatic(): AstroIntegration {
return {
name: '@astrojs/netlify',
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 }) => {
_config = config;
},
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/vercel/src/static/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function vercelStatic({
outDir,
build: {
format: 'directory',
redirects: false,
},
vite: {
define: viteDefine,
Expand Down