From cfce2d4be6edfd34812ae3f40983914924ec2dd1 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 6 Apr 2022 16:21:46 -0400 Subject: [PATCH] Fix Netlify adapter and dynamic routes (#3011) * Fix Netlify adapter and dynamic routes * Changeset --- src/@types/astro.ts | 7 +++++++ src/core/routing/manifest/create.ts | 19 +++++++------------ src/core/routing/manifest/serialization.ts | 10 ++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/@types/astro.ts b/src/@types/astro.ts index b0563d9aa0d4..76e24a4a0fca 100644 --- a/src/@types/astro.ts +++ b/src/@types/astro.ts @@ -798,12 +798,19 @@ export interface AstroIntegration { export type RouteType = 'page' | 'endpoint'; +export interface RoutePart { + content: string; + dynamic: boolean; + spread: boolean; +} + export interface RouteData { component: string; generate: (data?: any) => string; params: string[]; pathname?: string; pattern: RegExp; + segments: RoutePart[][]; type: RouteType; } diff --git a/src/core/routing/manifest/create.ts b/src/core/routing/manifest/create.ts index ec1d6a578c64..9555d9753eca 100644 --- a/src/core/routing/manifest/create.ts +++ b/src/core/routing/manifest/create.ts @@ -1,4 +1,4 @@ -import type { AstroConfig, ManifestData, RouteData } from '../../../@types/astro'; +import type { AstroConfig, ManifestData, RouteData, RoutePart } from '../../../@types/astro'; import type { LogOptions } from '../../logger/core'; import fs from 'fs'; @@ -9,16 +9,10 @@ import { fileURLToPath } from 'url'; import { warn } from '../../logger/core.js'; import { resolvePages } from '../../util.js'; -interface Part { - content: string; - dynamic: boolean; - spread: boolean; -} - interface Item { basename: string; ext: string; - parts: Part[]; + parts: RoutePart[]; file: string; isDir: boolean; isIndex: boolean; @@ -35,7 +29,7 @@ function countOccurrences(needle: string, haystack: string) { } function getParts(part: string, file: string) { - const result: Part[] = []; + const result: RoutePart[] = []; part.split(/\[(.+?\(.+?\)|.+?)\]/).map((str, i) => { if (!str) return; const dynamic = i % 2 === 1; @@ -56,7 +50,7 @@ function getParts(part: string, file: string) { return result; } -function getPattern(segments: Part[][], addTrailingSlash: AstroConfig['trailingSlash']) { +function getPattern(segments: RoutePart[][], addTrailingSlash: AstroConfig['trailingSlash']) { const pathname = segments .map((segment) => { return segment[0].spread @@ -94,7 +88,7 @@ function getTrailingSlashPattern(addTrailingSlash: AstroConfig['trailingSlash']) return '\\/?$'; } -function getGenerator(segments: Part[][], addTrailingSlash: AstroConfig['trailingSlash']) { +function getGenerator(segments: RoutePart[][], addTrailingSlash: AstroConfig['trailingSlash']) { const template = segments .map((segment) => { return segment[0].spread @@ -181,7 +175,7 @@ export function createRouteManifest( const validPageExtensions: Set = new Set(['.astro', '.md']); const validEndpointExtensions: Set = new Set(['.js', '.ts']); - function walk(dir: string, parentSegments: Part[][], parentParams: string[]) { + function walk(dir: string, parentSegments: RoutePart[][], parentParams: string[]) { let items: Item[] = []; fs.readdirSync(dir).forEach((basename) => { const resolved = path.join(dir, basename); @@ -285,6 +279,7 @@ export function createRouteManifest( routes.push({ type: item.isPage ? 'page' : 'endpoint', pattern, + segments, params, component, generate, diff --git a/src/core/routing/manifest/serialization.ts b/src/core/routing/manifest/serialization.ts index e167eb374e76..fcd932738bed 100644 --- a/src/core/routing/manifest/serialization.ts +++ b/src/core/routing/manifest/serialization.ts @@ -1,11 +1,12 @@ -import type { RouteData, SerializedRouteData } from '../../../@types/astro'; +import type { RouteData, SerializedRouteData, RoutePart } from '../../../@types/astro'; function createRouteData( pattern: RegExp, params: string[], component: string, pathname: string | undefined, - type: 'page' | 'endpoint' + type: 'page' | 'endpoint', + segments: RoutePart[][] ): RouteData { return { type, @@ -15,6 +16,7 @@ function createRouteData( // TODO bring back generate: () => '', pathname: pathname || undefined, + segments }; } @@ -26,7 +28,7 @@ export function serializeRouteData(routeData: RouteData): SerializedRouteData { } export function deserializeRouteData(rawRouteData: SerializedRouteData) { - const { component, params, pathname, type } = rawRouteData; + const { component, params, pathname, type, segments } = rawRouteData; const pattern = new RegExp(rawRouteData.pattern); - return createRouteData(pattern, params, component, pathname, type); + return createRouteData(pattern, params, component, pathname, type, segments); }