-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prioritize dynamic prerendered routes over dynamic server routes (…
…#7235) * test: add unit tests * fix: prioritize prerendered routes * chore: fix test * add comment * test: try avoiding race condition * chore: changeset * try avoiding race conditions attempt #2 * try avoiding race conditions (attempt 3) * final fix hopefuly * tet: add more tests * sort conflicting dynamic routes aplhabetically * test: fix test
- Loading branch information
1 parent
e20a3b2
commit ee2aca8
Showing
6 changed files
with
361 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Prioritize dynamic prerendered routes over dynamic server routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { AstroSettings, RouteData } from '../@types/astro'; | ||
import { preload, type DevelopmentEnvironment } from '../core/render/dev/index.js'; | ||
import { getPrerenderStatus } from './metadata.js'; | ||
|
||
type GetSortedPreloadedMatchesParams = { | ||
env: DevelopmentEnvironment; | ||
matches: RouteData[]; | ||
settings: AstroSettings; | ||
}; | ||
export async function getSortedPreloadedMatches({ | ||
env, | ||
matches, | ||
settings, | ||
}: GetSortedPreloadedMatchesParams) { | ||
return ( | ||
await preloadAndSetPrerenderStatus({ | ||
env, | ||
matches, | ||
settings, | ||
}) | ||
).sort((a, b) => prioritizePrerenderedMatchesComparator(a.route, b.route)); | ||
} | ||
|
||
type PreloadAndSetPrerenderStatusParams = { | ||
env: DevelopmentEnvironment; | ||
matches: RouteData[]; | ||
settings: AstroSettings; | ||
}; | ||
async function preloadAndSetPrerenderStatus({ | ||
env, | ||
matches, | ||
settings, | ||
}: PreloadAndSetPrerenderStatusParams) { | ||
const preloaded = await Promise.all( | ||
matches.map(async (route) => { | ||
const filePath = new URL(`./${route.component}`, settings.config.root); | ||
const preloadedComponent = await preload({ env, filePath }); | ||
|
||
// gets the prerender metadata set by the `astro:scanner` vite plugin | ||
const prerenderStatus = getPrerenderStatus({ | ||
filePath, | ||
loader: env.loader, | ||
}); | ||
|
||
if (prerenderStatus !== undefined) { | ||
route.prerender = prerenderStatus; | ||
} | ||
|
||
return { preloadedComponent, route, filePath } as const; | ||
}) | ||
); | ||
return preloaded; | ||
} | ||
|
||
function prioritizePrerenderedMatchesComparator(a: RouteData, b: RouteData): number { | ||
if (areRegexesEqual(a.pattern, b.pattern)) { | ||
if (a.prerender !== b.prerender) { | ||
return a.prerender ? -1 : 1; | ||
} | ||
return a.component < b.component ? -1 : 1; | ||
} | ||
return 0; | ||
} | ||
|
||
function areRegexesEqual(regexp1: RegExp, regexp2: RegExp) { | ||
return regexp1.source === regexp2.source && regexp1.global === regexp2.global; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.