Skip to content

Commit

Permalink
add changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jan 14, 2025
1 parent 2d5dce8 commit e0ed1bd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
20 changes: 20 additions & 0 deletions .changeset/light-pants-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'astro': minor
---

Adds support for external redirects `astro.config.mjs`:

```js
// astro.config.mjs
import {defineConfig} from "astro/config"

export default defineConfig({
redirects: {
"/blog": "https://example.com/blog",
"/news": {
status: 302,
destination: "https://example.com/news"
}
}
})
```
24 changes: 19 additions & 5 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
* and the value is the path to redirect to.
*
* You can redirect both static and dynamic routes, but only to the same kind of route.
* For example you cannot have a `'/article': '/blog/[...slug]'` redirect.
* For example, you cannot have a `'/article': '/blog/[...slug]'` redirect.
*
*
* ```js
* {
* export default defineConfig({
* redirects: {
* '/old': '/new',
* '/blog/[...slug]': '/articles/[...slug]',
* }
* }
* })
* ```
*
*
Expand All @@ -287,14 +287,28 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
* You can customize the [redirection status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages) using an object in the redirect config:
*
* ```js
* {
* export default defineConfig({
* redirects: {
* '/other': {
* status: 302,
* destination: '/place',
* },
* }
* }
* })
* ```
*
* Since **v5.2.0**, the property accepts external URLs:
*
* ```js
* export default defineConfig({
* redirects: {
* '/blog': 'https://example.com/blog',
* '/news': {
* status: 302,
* destination: 'https://example.com/news'
* }
* }
* })
* ```
*/
redirects?: Record<string, RedirectConfig>;
Expand Down
33 changes: 17 additions & 16 deletions packages/underscore-redirects/src/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ export function createRedirectsFromAstroRoutes({
dir,
buildOutput,
assets,
}: CreateRedirectsFromAstroRoutesParams) {
}: CreateRedirectsFromAstroRoutesParams): Redirects {
const base =
config.base && config.base !== '/'
? config.base.endsWith('/')
? config.base.slice(0, -1)
: config.base
: '';
const _redirects = new Redirects();
const redirects = new Redirects();

for (const [route, dynamicTarget = ''] of routeToDynamicTargetMap) {
const distURL = assets.get(route.pattern);
// A route with a `pathname` is as static route.
if (route.pathname) {
if (route.redirect) {
// A redirect route without dynami§c parts. Get the redirect status
// A redirect route without dynamic parts. Get the redirect status
// from the user if provided.
_redirects.add({
redirects.add({
dynamic: false,
input: `${base}${route.pathname}`,
target: typeof route.redirect === 'object' ? route.redirect.destination : route.redirect,
Expand All @@ -65,16 +65,18 @@ export function createRedirectsFromAstroRoutes({
// If this is a static build we don't want to add redirects to the HTML file.
if (buildOutput === 'static') {
continue;
} else if (distURL) {
_redirects.add({
}

if (distURL) {
redirects.add({
dynamic: false,
input: `${base}${route.pathname}`,
target: prependForwardSlash(distURL.toString().replace(dir.toString(), '')),
status: 200,
weight: 2,
});
} else {
_redirects.add({
redirects.add({
dynamic: false,
input: `${base}${route.pathname}`,
target: dynamicTarget,
Expand All @@ -83,7 +85,7 @@ export function createRedirectsFromAstroRoutes({
});

if (route.pattern === '/404') {
_redirects.add({
redirects.add({
dynamic: true,
input: '/*',
target: dynamicTarget,
Expand All @@ -100,22 +102,21 @@ export function createRedirectsFromAstroRoutes({
// This route was prerendered and should be forwarded to the HTML file.
if (distURL) {
const targetRoute = route.redirectRoute ?? route;
const targetPattern = generateDynamicPattern(targetRoute);
let target = targetPattern;
let target = generateDynamicPattern(targetRoute);
if (config.build.format === 'directory') {
target = pathJoin(target, 'index.html');
} else {
target += '.html';
}
_redirects.add({
redirects.add({
dynamic: true,
input: `${base}${pattern}`,
target,
status: route.type === 'redirect' ? 301 : 200,
weight: 1,
});
} else {
_redirects.add({
redirects.add({
dynamic: true,
input: `${base}${pattern}`,
target: dynamicTarget,
Expand All @@ -126,7 +127,7 @@ export function createRedirectsFromAstroRoutes({
}
}

return _redirects;
return redirects;
}

/**
Expand All @@ -135,7 +136,7 @@ export function createRedirectsFromAstroRoutes({
* With stars replacing spread and :id syntax replacing [id]
*/
function generateDynamicPattern(route: IntegrationResolvedRoute) {
const pattern =
return (
'/' +
route.segments
.map(([part]) => {
Expand All @@ -150,8 +151,8 @@ function generateDynamicPattern(route: IntegrationResolvedRoute) {
return part.content;
}
})
.join('/');
return pattern;
.join('/')
);
}

function prependForwardSlash(str: string) {
Expand Down

0 comments on commit e0ed1bd

Please sign in to comment.