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

Feat: show 404 when getStaticPaths doesn't match URL #2743

Merged
merged 22 commits into from
Mar 10, 2022

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Mar 9, 2022

Resolves #2226

Changes

image

  • Make a getPropsAndParams call from the Vite middleware
    • If successful, continue rendering as normal
    • Otherwise, Log the error as a warning and return a 404. This effectively moves all getStaticPaths errors from the browser into the console
  • Extract component preload call into Vite middleware in order to make eager getPropsAndParams call
  • Update eslint config to use TypeScript's no-shadow option. This is because of random linter issues on enums. See this thread

Testing

packages/astro/test/astro-get-static-paths.test.js - assert status codes for valid and invalid static path matches

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Mar 9, 2022

🦋 Changeset detected

Latest commit: 951ec46

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

This PR includes changesets to release 1 package
Name Type
astro 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 Mar 9, 2022
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

Looks good at first glance. I'd double check to make sure that the changes to eagerly calling getParamsAndProps and passing the result into ssr don't break anything in build. Those might be shared across dev and build?

@bholmesdev
Copy link
Contributor Author

Looks good at first glance. I'd double check to make sure that the changes to eagerly calling getParamsAndProps and passing the result into ssr don't break anything in build. Those might be shared across dev and build?

Thankfully not! ssr is only called by the Vite middleware, so builds should be unaffected by this change. We also don't pass the result of getParamsAndProps here; we rely on the cache getting populated to avoid double-calling. Verified this was working locally 👍

@github-actions github-actions bot added the test label Mar 9, 2022
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

Overall LGTM! Pleased with the direction you took this. 👍🏻
Left a few small comments that might tighten things up a bit.

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

Nice job, this ended up being very clean!

A few small changes but overall approved.

packages/astro/src/core/render/core.ts Outdated Show resolved Hide resolved
packages/astro/src/core/routing/manifest/create.ts Outdated Show resolved Hide resolved
packages/astro/src/vite-plugin-astro-server/index.ts Outdated Show resolved Hide resolved
@natemoo-re
Copy link
Member

Since this is the same CI failure that everything is hitting, I'm fine merging this! Go for it.

@bholmesdev bholmesdev merged commit a14075e into main Mar 10, 2022
@bholmesdev bholmesdev deleted the feat/get-static-paths-404 branch March 10, 2022 18:02
This was referenced Mar 10, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* WIP: return 404 for unmatched getStaticPaths route

* feat: regex on static paths to 404 in dev

* Revert "WIP: return 404 for unmatched getStaticPaths route"

This reverts commit 9c395a2.

* feat: call getParamsAndProps pre-ssr to catch errs

* fix: remove unused cache regex check

* fix: revert getPattern changes

* fix: remove unused preload props

* fix: log 404 for custom 404 pages

* refactor: rename fixture for clarity

* feat: add getStaticPaths status code tests

* fix: pas rootRelativeUrl to handle subpaths

* fix: update dev-routing tests from 500 -> 404

* refactor: make error handling more explicit

* lint: use typescript no shadow to fix enum issue

* chore: add changeset

* refactor: clarify test names

* refactor: remove variable reassignment

* fix: update dev-routing tests 500 > 404

* refactor: update test file structure

* Fix: revert to old logging

Co-authored-by: Nate Moore <[email protected]>

* Chore: use `const enum` instead

Co-authored-by: Nate Moore <[email protected]>

* chore: format

Co-authored-by: Nate Moore <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
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: Unmatched routes with getStaticPaths not redirecting to 404.astro
3 participants