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, meaning if you are currently using the:

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

You can simply remove this flag. If you have been waiting on using redirects before it stabilized, how you can do so. Checkout [the docs](https://docs.astro.build/en/core-concepts/routing/#redirects) to learn how to use the feature.
matthewp marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 1 addition & 23 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 @@ -458,7 +457,7 @@ export interface AstroUserConfig {
* @name redirects (Experimental)
matthewp marked this conversation as resolved.
Show resolved Hide resolved
* @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 @@ -101,8 +101,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 @@ -43,7 +43,6 @@ const ASTRO_CONFIG_DEFAULTS = {
redirects: {},
experimental: {
assets: false,
redirects: false,
},
} satisfies AstroUserConfig & { server: { open: boolean } };

Expand Down Expand Up @@ -212,7 +211,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 mime from 'mime';
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 @@ -144,16 +143,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 @@ -207,7 +196,6 @@ export async function handleRoute({
manifest,
});
}
throwIfRedirectNotAllowed(result.response, config);
await writeWebResponse(incomingResponse, result.response);
} else {
let contentType = 'text/plain';
Expand All @@ -230,7 +218,6 @@ export async function handleRoute({
}
} else {
const result = await renderPage(options);
throwIfRedirectNotAllowed(result, config);
return await writeSSRResult(request, result, incomingResponse);
await writeSSRResult(request, result, incomingResponse);
}
}
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 @@ -92,7 +92,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.
matthewp marked this conversation as resolved.
Show resolved Hide resolved

```js
import { defineConfig } from 'astro/config';
Expand All @@ -104,9 +104,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