summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2023-06-05 09:03:20 -0400
committerGravatar GitHub <noreply@github.com> 2023-06-05 09:03:20 -0400
commit57f8d14c027c30919363e12c664ccff4ed64d0fc (patch)
tree61817073af197b716af5f0d5b55a6bac34abecdc /packages/integrations/vercel/src
parentdd1a6b6c941aeb7af934bd12db22412af262f5a1 (diff)
downloadastro-57f8d14c027c30919363e12c664ccff4ed64d0fc.tar.gz
astro-57f8d14c027c30919363e12c664ccff4ed64d0fc.tar.zst
astro-57f8d14c027c30919363e12c664ccff4ed64d0fc.zip
Redirects (#7067)
* Redirects spike * Allow redirects in static mode * Support in Netlify as well * Adding a changeset * Rename file * Fix build problem * Refactor to be more modular * Fix location ref * Late test should only run in SSR * Support redirects in Netlify SSR configuration (#7167) * Implement support for dynamic routes in redirects (#7173) * Implement support for dynamic routes in redirects * Remove the .only * No need to special-case redirects in static build * Implement support for redirects config in the Vercel adapter (#7182) * Implement support for redirects config in the Vercel adapter * Remove unused condition * Move to a internal helper package * Add support for the object notation in redirects * Use status 308 for non-GET redirects (#7186) * Implement redirects in Cloudflare (#7198) * Implement redirects in Cloudflare * Fix build * Update tests b/c of new ordering * Debug issue * Use posix.join * Update packages/underscore-redirects/package.json Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Update based on review comments * Update broken test --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Test that redirects can come from middleware (#7213) * Test that redirects can come from middleware * Allow non-promise returns for middleware * Implement priority (#7210) * Refactor * Fix netlify test ordering * Fix ordering again * Redirects: Allow preventing the output of the static HTML file (#7245) * Do a simple push for priority * Adding changesets * Put the implementation behind a flag. * Self review * Update .changeset/chatty-actors-stare.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/astro/src/@types/astro.ts Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/astro/src/@types/astro.ts Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/astro/src/@types/astro.ts Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/astro/src/@types/astro.ts Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update docs on dynamic restrictions. * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Code review changes * Document netlify static adapter * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Slight reword * Update .changeset/twenty-suns-vanish.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Add a note about public/_redirects file * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/vercel/src')
-rw-r--r--packages/integrations/vercel/src/lib/redirects.ts58
-rw-r--r--packages/integrations/vercel/src/static/adapter.ts1
2 files changed, 44 insertions, 15 deletions
diff --git a/packages/integrations/vercel/src/lib/redirects.ts b/packages/integrations/vercel/src/lib/redirects.ts
index c11d74802..1ec19bfac 100644
--- a/packages/integrations/vercel/src/lib/redirects.ts
+++ b/packages/integrations/vercel/src/lib/redirects.ts
@@ -1,4 +1,9 @@
import type { AstroConfig, RouteData, RoutePart } from 'astro';
+import { appendForwardSlash } from '@astrojs/internal-helpers/path';
+import nodePath from 'node:path';
+
+const pathJoin = nodePath.posix.join;
+
// https://vercel.com/docs/project-configuration#legacy/routes
interface VercelRoute {
@@ -54,28 +59,51 @@ function getReplacePattern(segments: RoutePart[][]) {
return result;
}
+function getRedirectLocation(route: RouteData, config: AstroConfig): string {
+ if(route.redirectRoute) {
+ const pattern = getReplacePattern(route.redirectRoute.segments);
+ const path = (config.trailingSlash === 'always' ? appendForwardSlash(pattern) : pattern);
+ return pathJoin(config.base, path);
+ } else if(typeof route.redirect === 'object') {
+ return pathJoin(config.base, route.redirect.destination);
+ } else {
+ return pathJoin(config.base, route.redirect || '');
+ }
+}
+
+function getRedirectStatus(route: RouteData): number {
+ if(typeof route.redirect === 'object') {
+ return route.redirect.status;
+ }
+ return 301;
+}
+
export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRoute[] {
let redirects: VercelRoute[] = [];
- if (config.trailingSlash === 'always') {
- for (const route of routes) {
- if (route.type !== 'page' || route.segments.length === 0) continue;
- redirects.push({
- src: config.base + getMatchPattern(route.segments),
- headers: { Location: config.base + getReplacePattern(route.segments) + '/' },
- status: 308,
- });
- }
- } else if (config.trailingSlash === 'never') {
- for (const route of routes) {
- if (route.type !== 'page' || route.segments.length === 0) continue;
+ for(const route of routes) {
+ if(route.type === 'redirect') {
redirects.push({
- src: config.base + getMatchPattern(route.segments) + '/',
- headers: { Location: config.base + getReplacePattern(route.segments) },
- status: 308,
+ src: config.base + getMatchPattern(route.segments),
+ headers: { Location: getRedirectLocation(route, config) },
+ status: getRedirectStatus(route)
});
+ } else if (route.type === 'page') {
+ if (config.trailingSlash === 'always') {
+ redirects.push({
+ src: config.base + getMatchPattern(route.segments),
+ headers: { Location: config.base + getReplacePattern(route.segments) + '/' },
+ status: 308,
+ });
+ } else if (config.trailingSlash === 'never') {
+ redirects.push({
+ src: config.base + getMatchPattern(route.segments) + '/',
+ headers: { Location: config.base + getReplacePattern(route.segments) },
+ status: 308,
+ });
+ }
}
}
diff --git a/packages/integrations/vercel/src/static/adapter.ts b/packages/integrations/vercel/src/static/adapter.ts
index 0b3579cdd..e0cc14322 100644
--- a/packages/integrations/vercel/src/static/adapter.ts
+++ b/packages/integrations/vercel/src/static/adapter.ts
@@ -43,6 +43,7 @@ export default function vercelStatic({
outDir,
build: {
format: 'directory',
+ redirects: false,
},
vite: {
define: viteDefine,