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

fix(astro): ignore prerendered pages in middleware #4640

Merged

Conversation

jlengstorf
Copy link
Contributor

@jlengstorf jlengstorf commented Nov 23, 2024

fixes #4639

Description

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented Nov 23, 2024

🦋 Changeset detected

Latest commit: 8b9eff5

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

This PR includes changesets to release 1 package
Name Type
@clerk/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

Copy link

vercel bot commented Nov 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 24, 2024 6:44pm

@jlengstorf jlengstorf changed the title fix: ignore prerendred pages in middleware fix: ignore prerendered pages in middleware Nov 23, 2024
@tmilewski tmilewski self-assigned this Nov 24, 2024
@tmilewski tmilewski self-requested a review November 24, 2024 18:38
@tmilewski tmilewski changed the title fix: ignore prerendered pages in middleware fix(astro): ignore prerendered pages in middleware Nov 24, 2024
@tmilewski tmilewski merged commit 75cb50c into clerk:main Nov 24, 2024
8 of 23 checks passed
@tmilewski
Copy link
Member

Thanks, @jlengstorf!

@wobsoriano
Copy link
Member

wobsoriano commented Nov 25, 2024

Thanks @jlengstorf!

In case you still want to protect CSR pages, you can use the exported stores:

---
export async function getStaticPaths() {
  // other code
}
---

<!-- other code -->

<script>
import { $authStore } from '@clerk/astro/client'

$authStore.subscribe((auth) => {
  if (auth.userId === undefined) {
    // loading
  } else if (auth.userId === null) {
    // redirect to sign-in page
  }
})
</script>

@jlengstorf jlengstorf deleted the fix-4639/astro-ignore-prerendered-pages branch November 26, 2024 06:06
@mlafeldt
Copy link
Contributor

mlafeldt commented Dec 2, 2024

As far as I know, isPrerendered will be added in Astro 5, right?

Right now, the property is called _isPrerendered.

See withastro/astro#11756

@wobsoriano
Copy link
Member

wobsoriano commented Dec 2, 2024

As far as I know, isPrerendered will be added in Astro 5, right?

Right now, the property is called _isPrerendered.

See withastro/astro#11756

Thanks for this! Is main branch v4? I think they ported isPrerendered to v4 here

Okay, I can confirm it's still _isPrerendered in latest v4 version. Will do a follow up PR.

@mlafeldt
Copy link
Contributor

mlafeldt commented Dec 2, 2024

Thanks for this! Is main branch v4? I think they ported isPrerendered to v4 here

Hm, I don't think so. I'm using Astro v4.16.16 and all I see is _isPrerendered.

❯ rg --no-ignore isPrerendered node_modules/astro
node_modules/astro/dist/actions/runtime/middleware.js
11:  if (context._isPrerendered) {

node_modules/astro/dist/core/render-context.js
95:    const isPrerendered = !serverLike || this.routeData.prerender;
104:    const apiContext = this.createAPIContext(props, isPrerendered);
185:  createAPIContext(props, isPrerendered) {
198:      _isPrerendered: isPrerendered

node_modules/astro/dist/core/render-context.d.ts
46:    createAPIContext(props: APIContext['props'], isPrerendered: boolean): APIContext;

See https://github.com/withastro/astro/blob/astro%404.16.16/packages/astro/src/core/render-context.ts#L248

@mlafeldt
Copy link
Contributor

mlafeldt commented Dec 2, 2024

Okay, I can confirm it's still _isPrerendered in latest v4 version. Will do a follow up PR.

@wobsoriano Cool. Something like this should work for all versions:

const isPrerendered = (context: any) => context._isPrerendered || context.isPrerendered

@jlengstorf
Copy link
Contributor Author

sorry about that! I'm using Astro 5 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prerendered pages in Astro throw a warning when using clerkMiddleware
4 participants