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

Updates the dev server to handle multiple routes matching the same URL #4087

Merged
merged 10 commits into from
Jul 29, 2022

Conversation

tony-sull
Copy link
Contributor

@tony-sull tony-sull commented Jul 28, 2022

Closes #3809

Changes

  • dev server now finds all page templates that match the requested URL and checks each one in priority order

    • in SSR, users will get an error if more than one page template matches a URL in astro dev
    • in SSG, the dev server now prioritizes URLs the same as astro build
  • injected routes are now sorted with a higher priority than all user-provided page routes

Testing

  • all existing routing-priority tests pass
  • added tests for prioritizing injected routes
  • added tests for multiple page routes matching the same URL

Docs

N/A bug fix only

@changeset-bot
Copy link

changeset-bot bot commented Jul 28, 2022

🦋 Changeset detected

Latest commit: 03a253e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
astro Patch
@e2e/astro-component Patch
@e2e/error-react-spectrum Patch
@e2e/error-sass Patch
@e2e/errors Patch
@e2e/lit-component Patch
@e2e/preact-component Patch
@e2e/react-component Patch
@e2e/solid-component Patch
@e2e/svelte-component Patch
@e2e/e2e-tailwindcss Patch
@e2e/ts-resolution Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jul 28, 2022
@tony-sull tony-sull self-assigned this Jul 29, 2022
@tony-sull
Copy link
Contributor Author

tony-sull commented Jul 29, 2022

Likely related to #3809 as well, dev routing should prioritize injected routes above all src/pages routes

@tony-sull tony-sull marked this pull request as ready for review July 29, 2022 19:53
@tony-sull tony-sull changed the title WIP: Updates the dev server to handle multiple routes matching the same URL Updates the dev server to handle multiple routes matching the same URL Jul 29, 2022
@@ -159,6 +159,29 @@ function comparator(a: Item, b: Item) {
return a.file < b.file ? -1 : 1;
}

function injectedRouteToItem(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to sort injected routes with the same comparer used for user-defined routes

@@ -4,3 +4,8 @@ import type { ManifestData, RouteData } from '../../@types/astro';
export function matchRoute(pathname: string, manifest: ManifestData): RouteData | undefined {
return manifest.routes.find((route) => route.pattern.test(pathname));
}

/** Finds all matching routes from pathname */
export function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used by the dev server to find all potential routes for the requested URL

const filePath = new URL(`./${maybeRoute.component}`, config.root);
const preloadedComponent = await preload({ astroConfig: config, filePath, viteServer });
const [, mod] = preloadedComponent;
// attempt to get static paths
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this result in getStaticPaths being called more than once?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still only called once. The getStaticPaths() result is always cached even if the params don't match

Just noticed we have a test to make sure it's only called once but that test only covers build. I just pushed up a test that checks that for dev as well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: injectRoute path intercepted by dynamic route during dev
2 participants