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(prerender): support quoted true / false values and 0 / 1 #6772

Merged
merged 2 commits into from
Apr 6, 2023

Conversation

natemoo-re
Copy link
Member

@natemoo-re natemoo-re commented Apr 6, 2023

Changes

  • See https://discord.com/channels/830184174198718474/1067550590512676874/1067550590512676874 for context
  • Adds export const prerender support for quoted values of "true" | "false" | 'true' | 'false' as well as the literal values 0, 1.
  • This is helpful because Astro replaces import.meta.env.MY_VALUE values statically before prerender is scanned for.
  • This should unblock users to control prerendering by using an environment variable like
    PRERENDER_ENABLED=true
    
    ---
    export const prerender = import.meta.env.PRERENDER_ENABLED
    ---

Testing

Scan function unit tests have been updated to check "true" | "false" | 'true' | 'false' and 0 | 1. Did not test import.meta.env.MY_VALUE replacement directly, but can if that seems wise.

Docs

This is mostly intended to unblock users that have tried this already, not sure if we want to document this as an explicit feature yet since we might end up with a different pattern for "preview deploys".

@changeset-bot
Copy link

changeset-bot bot commented Apr 6, 2023

🦋 Changeset detected

Latest commit: 0c36095

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

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 Apr 6, 2023
Copy link
Member

@Princesseuh Princesseuh left a comment

Choose a reason for hiding this comment

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

Agree with not documenting this explicitly! LGTM

@edv-ns
Copy link
Contributor

edv-ns commented Apr 6, 2023

Looks neat 👍

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Let's 🚢 it!

@natemoo-re natemoo-re merged commit 45bff6f into main Apr 6, 2023
@natemoo-re natemoo-re deleted the feat/prerender-vars branch April 6, 2023 16:36
@astrobot-houston astrobot-houston mentioned this pull request Apr 6, 2023
@samducker
Copy link

Hi @natemoo-re

When shipping this I'm getting the following error

@website:build:  error   A `prerender` export has been detected, but its value cannot be statically analyzed.
@website:build:   Expected `true` value but got `process.env.PRERENDER_ENABLED`.
@website:build:   Hint:
@website:build:     Mutable values declared at runtime are not supported. Please make sure to use exactly `export const prerender = true`.
@website:build:   File:
@website:build:     /Users/sam/Documents/GitHub/website/repo/apps/marketing-website/src/pages/about.astro
@website:build:   Stacktrace:
@website:build: InvalidPrerenderExport: A `prerender` export has been detected, but its value cannot be statically analyzed.
@website:build: Expected `true` value but got `process.env.PRERENDER_ENABLED`.
@website:build:     at scan (file:///Users/sam/Documents/GitHub/website/repo/node_modules/.pnpm/astro@2.3.0_@types+node@18.15.13_sharp@0.31.3/node_modules/astro/dist/vite-plugin-scanner/scan.js:42:15)
@website:build:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
@website:build:     at async Object.transform (file:///Users/sam/Documents/GitHub/website/repo/node_modules/.pnpm/astro@2.3.0_@types+node@18.15.13_sharp@0.31.3/node_modules/astro/dist/vite-plugin-scanner/index.js:22:27)

And about.astro

export const prerender = import.meta.env.PRERENDER_ENABLED;

Astro version 2.3.0

@natemoo-re
Copy link
Member Author

Thanks for the report @samducker! I just opened #6910 which should fix this.

@samducker
Copy link

Thanks @natemoo-re appreciate you, this will help our content editors lives greatly 🚀

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.

5 participants