Skip to content

fix: use lazy promise on getCartCount to defer cookies() execution#1880

Merged
fikrikarim merged 1 commit intosoul/mainfrom
fikri/vib-1052-calling-cookies-in-a-streamed-function-results-in-a-500
Jan 9, 2025
Merged

fix: use lazy promise on getCartCount to defer cookies() execution#1880
fikrikarim merged 1 commit intosoul/mainfrom
fikri/vib-1052-calling-cookies-in-a-streamed-function-results-in-a-500

Conversation

@fikrikarim
Copy link
Copy Markdown
Member

@fikrikarim fikrikarim commented Jan 9, 2025

What/Why?

This fixes 500 error:

needs to bail out of prerendering at this point because it used cookies()

Without the lazy promise, the getCartCount() is immediately executed, and calls cookies() on the Header.

With lazy promise, the getCartCount() execution is deferred, until await/then is used.

Testing

This bug was only reproducible if we reverted the temporary fix commit on the soul/makeswift/integration branch. See this deployment.

Makeswift pages are PPRed correctly instead of being server-rendered

@bigcommerce/catalyst-core:build: ├ ◐ /[locale]                                  1.8 kB          441 kB
@bigcommerce/catalyst-core:build: ├   ├ /[locale]
@bigcommerce/catalyst-core:build: ├   └ /en
@bigcommerce/catalyst-core:build: ├ ◐ /[locale]/[...rest]                        1.8 kB          441 kB
@bigcommerce/catalyst-core:build: ├   ├ /[locale]/[...rest]
@bigcommerce/catalyst-core:build: ├   ├ /en/[...rest]
@bigcommerce/catalyst-core:build: ├   ├ /en/blog
@bigcommerce/catalyst-core:build: ├   └ [+5 more paths]

No 500 error logs:
image

This fixes 500 error: needs to bail out of prerendering at this point because it used cookies()

Without the lazy promise, the getCartCount() is immediately executed, and calls cookies().

With lazy promise, the getCartCount() execution is deferred, until await/then is used.
@fikrikarim fikrikarim requested a review from a team January 9, 2025 15:17
@linear
Copy link
Copy Markdown

linear Bot commented Jan 9, 2025

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 9, 2025

⚠️ No Changeset found

Latest commit: dc579a8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 9, 2025

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

Name Status Preview Comments Updated (UTC)
catalyst-latest ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 9, 2025 3:17pm
catalyst-soul ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 9, 2025 3:17pm
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
catalyst ⬜️ Ignored (Inspect) Jan 9, 2025 3:17pm
catalyst-au ⬜️ Ignored (Inspect) Jan 9, 2025 3:17pm
catalyst-uk ⬜️ Ignored (Inspect) Jan 9, 2025 3:17pm

@fikrikarim fikrikarim requested a review from migueloller January 9, 2025 15:18
Copy link
Copy Markdown
Contributor

@migueloller migueloller left a comment

Choose a reason for hiding this comment

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

Thanks!

Looking at this makes me wonder if we should handle this in VIBES at some point, where we take the approach of accepting functions and we turn them into lazy promises 🤔

@fikrikarim fikrikarim merged commit d4e9bf2 into soul/main Jan 9, 2025
@fikrikarim fikrikarim deleted the fikri/vib-1052-calling-cookies-in-a-streamed-function-results-in-a-500 branch January 9, 2025 17:01
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.

2 participants