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

Remove experimental flag for redirects config #7686

Merged
merged 11 commits into from
Jul 19, 2023
15 changes: 15 additions & 0 deletions .changeset/dry-beers-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'astro': minor
---

Redirects configuration

This change moves the `redirects` configuration out of experimental. If you were previously using experimental redirects, remove the following experimental flag:

```js
experimental: {
redirects: true,
}
matthewp marked this conversation as resolved.
Show resolved Hide resolved
```

If you have been waiting for stabilization before using redirects, now you can do so. Check out [the docs on redirects](https://docs.astro.build/en/core-concepts/routing/#redirects) to learn how to use this built-in feature.
26 changes: 2 additions & 24 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export interface CLIFlags {
drafts?: boolean;
open?: boolean;
experimentalAssets?: boolean;
experimentalRedirects?: boolean;
}

/**
Expand Down Expand Up @@ -455,10 +454,10 @@ export interface AstroUserConfig {

/**
* @docs
* @name redirects (Experimental)
* @name redirects
* @type {Record<string, RedirectConfig>}
* @default `{}`
* @version 2.6.0
* @version 2.9.0
* @description Specify a mapping of redirects where the key is the route to match
* and the value is the path to redirect to.
*
Expand Down Expand Up @@ -1228,27 +1227,6 @@ export interface AstroUserConfig {
* ```
*/
assets?: boolean;

/**
* @docs
* @name experimental.redirects
* @type {boolean}
* @default `false`
* @version 2.6.0
* @description
* Enable experimental support for redirect configuration. With this enabled
* you can set redirects via the top-level `redirects` property. To enable
* this feature, set `experimental.redirects` to `true`.
*
* ```js
* {
* experimental: {
* redirects: true,
* },
* }
* ```
*/
redirects?: boolean;
};

// Legacy options to be removed
Expand Down
8 changes: 1 addition & 7 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ import {
import { runHookBuildGenerated } from '../../integrations/index.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { callEndpoint, throwIfRedirectNotAllowed } from '../endpoint/index.js';
import { callEndpoint } from '../endpoint/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { debug, info } from '../logger/core.js';
import {
getRedirectLocationOrThrow,
RedirectSinglePageBuiltModule,
routeIsRedirect,
} from '../redirects/index.js';
import { createEnvironment, createRenderContext, tryRenderPage } from '../render/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
Expand Down Expand Up @@ -228,10 +227,6 @@ async function generatePage(
builtPaths: Set<string>,
manifest: SSRManifest
) {
if (routeIsRedirect(pageData.route) && !opts.settings.config.experimental.redirects) {
throw new Error(`To use redirects first set experimental.redirects to \`true\``);
}

let timeStart = performance.now();

const pageInfo = getPageDataByComponent(internals, pageData.route.component);
Expand Down Expand Up @@ -561,7 +556,6 @@ async function generatePath(
);

if (result.type === 'response') {
throwIfRedirectNotAllowed(result.response, opts.settings.config);
// If there's no body, do nothing
if (!result.response.body) return;
const ab = await result.response.arrayBuffer();
Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
drafts: typeof flags.drafts === 'boolean' ? flags.drafts : undefined,
experimentalAssets:
typeof flags.experimentalAssets === 'boolean' ? flags.experimentalAssets : undefined,
experimentalRedirects:
typeof flags.experimentalRedirects === 'boolean' ? flags.experimentalRedirects : undefined,
};
}

Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const ASTRO_CONFIG_DEFAULTS = {
redirects: {},
experimental: {
assets: false,
redirects: false,
},
} satisfies AstroUserConfig & { server: { open: boolean } };

Expand Down Expand Up @@ -233,7 +232,6 @@ export const AstroConfigSchema = z.object({
experimental: z
.object({
assets: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.assets),
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.redirects),
})
.passthrough()
.refine(
Expand Down
16 changes: 0 additions & 16 deletions packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
APIContext,
AstroConfig,
EndpointHandler,
EndpointOutput,
MiddlewareEndpointHandler,
Expand All @@ -9,7 +8,6 @@ import type {
} from '../../@types/astro';
import type { Environment, RenderContext } from '../render/index';

import { isServerLikeOutput } from '../../prerender/utils.js';
import { renderEndpoint } from '../../runtime/server/index.js';
import { ASTRO_VERSION } from '../constants.js';
import { AstroCookies, attachToResponse } from '../cookies/index.js';
Expand Down Expand Up @@ -161,17 +159,3 @@ export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>
cookies: context.cookies,
};
}

function isRedirect(statusCode: number) {
return statusCode >= 300 && statusCode < 400;
}

export function throwIfRedirectNotAllowed(response: Response, config: AstroConfig) {
if (
!isServerLikeOutput(config) &&
isRedirect(response.status) &&
!config.experimental.redirects
) {
throw new AstroError(AstroErrorData.StaticRedirectNotAvailable);
}
}
15 changes: 1 addition & 14 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type http from 'node:http';
import type { ComponentInstance, ManifestData, RouteData, SSRManifest } from '../@types/astro';
import { attachToResponse } from '../core/cookies/index.js';
import { call as callEndpoint } from '../core/endpoint/dev/index.js';
import { throwIfRedirectNotAllowed } from '../core/endpoint/index.js';
import { AstroErrorData, isAstroError } from '../core/errors/index.js';
import { warn } from '../core/logger/core.js';
import { loadMiddleware } from '../core/middleware/loadMiddleware.js';
Expand Down Expand Up @@ -146,16 +145,6 @@ export async function handleRoute({
return handle404Response(origin, incomingRequest, incomingResponse);
}

if (matchedRoute.route.type === 'redirect' && !settings.config.experimental.redirects) {
writeWebResponse(
incomingResponse,
new Response(`To enable redirect set experimental.redirects to \`true\`.`, {
status: 400,
})
);
return;
}

const { config } = settings;
const filePath: URL | undefined = matchedRoute.filePath;
const { route, preloadedComponent } = matchedRoute;
Expand Down Expand Up @@ -210,7 +199,6 @@ export async function handleRoute({
manifest,
});
}
throwIfRedirectNotAllowed(result.response, config);
await writeWebResponse(incomingResponse, result.response);
} else {
let contentType = 'text/plain';
Expand Down Expand Up @@ -249,14 +237,13 @@ export async function handleRoute({
manifest,
});
}
throwIfRedirectNotAllowed(result, config);

let response = result;
// Response.status is read-only, so a clone is required to override
if (status && response.status !== status) {
response = new Response(result.body, { ...result, status });
}
return await writeSSRResult(request, response, incomingResponse);
await writeSSRResult(request, response, incomingResponse);
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/astro/test/dev-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ describe('Development Routing', () => {
const response = await fixture.fetch('/2');
expect(response.status).to.equal(404);
});

it('500 when redirecting in SSG mode', async () => {
const response = await fixture.fetch('/redirect');
expect(response.status).to.equal(500);
});
});

describe('No subpath used', () => {
Expand Down
12 changes: 0 additions & 12 deletions packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ describe('Astro.redirect', () => {
redirects: {
'/api/redirect': '/test',
},
experimental: {
redirects: true,
},
});
await fixture.build();
});
Expand Down Expand Up @@ -71,9 +68,6 @@ describe('Astro.redirect', () => {
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
output: 'static',
experimental: {
redirects: true,
},
redirects: {
'/': '/test',
'/one': '/test',
Expand Down Expand Up @@ -159,9 +153,6 @@ describe('Astro.redirect', () => {
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
output: 'static',
experimental: {
redirects: true,
},
redirects: {
'/one': '/',
},
Expand Down Expand Up @@ -194,9 +185,6 @@ describe('Astro.redirect', () => {
build: {
redirects: false,
},
experimental: {
redirects: true,
},
});
await fixture.build();
});
Expand Down
3 changes: 0 additions & 3 deletions packages/integrations/cloudflare/test/directory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ describe('mode: "directory"', () => {
redirects: {
'/old': '/',
},
experimental: {
redirects: true,
},
});
await fixture.build();
});
Expand Down
5 changes: 1 addition & 4 deletions packages/integrations/netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default defineConfig({

### Static sites

For static sites you usually don't need an adapter. However, if you use `redirects` configuration (experimental) in your Astro config, the Netlify adapter can be used to translate this to the proper `_redirects` format.
For static sites you usually don't need an adapter. However, if you use `redirects` configuration in your Astro config, the Netlify adapter can be used to translate this to the proper `_redirects` format.

```js
import { defineConfig } from 'astro/config';
Expand All @@ -154,9 +154,6 @@ export default defineConfig({
redirects: {
'/blog/old-post': '/blog/new-post',
},
experimental: {
redirects: true,
},
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ describe('SSG - Redirects', () => {
redirects: {
'/other': '/',
},
experimental: {
redirects: true,
},
});
await fixture.build();
});
Expand Down
3 changes: 0 additions & 3 deletions packages/integrations/netlify/test/static/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ describe('SSG - Redirects', () => {
root: new URL('./fixtures/redirects/', import.meta.url).toString(),
output: 'static',
adapter: netlifyStatic(),
experimental: {
redirects: true,
},
site: `http://example.com`,
integrations: [testIntegration()],
redirects: {
Expand Down
3 changes: 0 additions & 3 deletions packages/integrations/vercel/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ describe('Redirects', () => {
'/blog/[...slug]': '/team/articles/[...slug]',
},
trailingSlash: 'always',
experimental: {
redirects: true,
},
});
await fixture.build();
});
Expand Down