Skip to content

Conversation

danielroe
Copy link
Member

🔗 Linked issue

resolves #33124

📚 Description

we do not always have window globally available

I'll investigate also to see if there's anything else we can helpfully swap out for web workers

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

coderabbitai bot commented Sep 3, 2025

Walkthrough

Client-side references to the global environment were changed from window to globalThis across three files: packages/nuxt/src/app/compat/interval.ts (exported setInterval), packages/nuxt/src/app/composables/cookie.ts (initialising cookieStore), and packages/nuxt/src/app/composables/url.ts (using location.href in useRequestURL). Comments were added explaining use of globalThis to avoid crashes in web workers. Behaviour, control flow and public APIs were preserved; only the global object reference and explanatory comments were updated.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/set-interval-worker

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Sep 3, 2025

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@33125

nuxt

npm i https://pkg.pr.new/nuxt@33125

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@33125

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@33125

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@33125

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@33125

commit: 3e15728

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/nuxt/src/app/compat/interval.ts (3)

6-6: Optional: cast to DOM typing to avoid NodeJS.Timeout bleed-through

In repos that include @types/node alongside DOM libs, global typings can merge and surface NodeJS.Timeout at call sites. Casting the client branch keeps the familiar DOM number return in editors/TS tooling.

Apply:

-export const setInterval = import.meta.client
-  ? globalThis.setInterval
+export const setInterval = import.meta.client
+  ? (globalThis.setInterval as typeof window.setInterval)
   : () => {

3-3: Nit: add a brief JSDoc noting client-only usage

A one-liner helps consumers understand the server behaviour quickly.

-const intervalError = '[nuxt] `setInterval` should not be used on the server. Consider wrapping it with an `onNuxtReady`, `onBeforeMount` or `onMounted` lifecycle hook, or ensure you only call it in the browser by checking `import.meta.client`.'
+/** Client-only alias for setInterval; throws in dev and logs in prod on the server. */
+const intervalError = '[nuxt] `setInterval` should not be used on the server. Consider wrapping it with an `onNuxtReady`, `onBeforeMount` or `onMounted` lifecycle hook, or ensure you only call it in the browser by checking `import.meta.client`.'

5-16: Audit complete—no window. timers found, but NodeJS.Timeout types present*

  • No other window.setTimeout/setInterval or manual shims detected beyond packages/nuxt/src/app/compat/interval.ts.
  • In cookie.ts, loading-indicator.ts and fetch.ts, timer returns are typed as NodeJS.Timeout—consider replacing with number or defining a shared Timer alias under app/compat for consistency and browser compatibility.
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 418bafe and d68b6e6.

📒 Files selected for processing (1)
  • packages/nuxt/src/app/compat/interval.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/nuxt/src/app/compat/interval.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: code
🔇 Additional comments (1)
packages/nuxt/src/app/compat/interval.ts (1)

5-7: Use of globalThis fixes Worker context — LGTM

Switching from window.setInterval to globalThis.setInterval correctly handles Web Workers while preserving the server guard. Good, targeted fix.

@danielroe danielroe changed the title fix(nuxt): access setInterval from globalThis fix(nuxt): prefer accessing globalThis over window Sep 3, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/nuxt/src/app/composables/cookie.ts (2)

174-180: Guard document access for Worker contexts

useCookie currently throws in Web Workers because readRawCookies touches document.cookie when import.meta.client is true. Add a runtime guard.

Apply:

-  } else if (import.meta.client) {
-    return parse(document.cookie, opts)
+  } else if (import.meta.client) {
+    if (typeof document !== 'undefined') {
+      return parse(document.cookie, opts)
+    }
+    // In non-Window clients (e.g. Web Workers) document is unavailable
+    return undefined
   }

189-193: Mirror the Worker guard when writing cookies

Protect writeClientCookie with the same document check to avoid Worker runtime errors if useCookie is used there.

Apply:

-  if (import.meta.client) {
-    document.cookie = serializeCookie(name, value, opts)
-  }
+  if (import.meta.client && typeof document !== 'undefined') {
+    document.cookie = serializeCookie(name, value, opts)
+  }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d68b6e6 and 3e15728.

📒 Files selected for processing (2)
  • packages/nuxt/src/app/composables/cookie.ts (1 hunks)
  • packages/nuxt/src/app/composables/url.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/nuxt/src/app/composables/cookie.ts
  • packages/nuxt/src/app/composables/url.ts
🧠 Learnings (1)
📓 Common learnings
Learnt from: GalacticHypernova
PR: nuxt/nuxt#26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: code
🔇 Additional comments (2)
packages/nuxt/src/app/composables/url.ts (1)

9-10: Worker-safe global: LGTM

Switching to globalThis avoids Worker crashes and preserves behaviour.

packages/nuxt/src/app/composables/cookie.ts (1)

36-37: Worker-safe cookieStore access: LGTM

Using globalThis.cookieStore aligns with the PR goal and gracefully falls back to BroadcastChannel when unavailable.

Copy link

codspeed-hq bot commented Sep 3, 2025

CodSpeed Performance Report

Merging #33125 will not alter performance

Comparing fix/set-interval-worker (3e15728) with main (418bafe)

Summary

✅ 10 untouched benchmarks

@danielroe danielroe merged commit c73957a into main Sep 3, 2025
47 checks passed
@danielroe danielroe deleted the fix/set-interval-worker branch September 3, 2025 11:54
This was referenced Sep 3, 2025
This was referenced Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A polyfill is automatically injected into Web Workers that use setInterval, breaking them

1 participant