Skip to content

Comments

fix: reuse nuxt instance at plugin setup#3690

Merged
BobbieGoede merged 4 commits intov9from
fix/nuxt-app-usage
Jun 25, 2025
Merged

fix: reuse nuxt instance at plugin setup#3690
BobbieGoede merged 4 commits intov9from
fix/nuxt-app-usage

Conversation

@BobbieGoede
Copy link
Member

@BobbieGoede BobbieGoede commented Jun 12, 2025

🔗 Linked issue

📚 Description

Supersedes #3689
Resolves nuxt-modules/storybook#899

Summary by CodeRabbit

  • Refactor
    • Improved handling of the Nuxt app context by explicitly passing the Nuxt app instance to locale detection and switching functions.
    • Enhanced clarity and maintainability by removing hidden dependencies on global state within localization-related features.

No visible changes to end-user functionality.

@BobbieGoede BobbieGoede self-assigned this Jun 12, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 12, 2025

Walkthrough

The changes refactor several i18n-related utilities and plugins to require an explicit NuxtApp instance as a parameter, replacing previous reliance on internal or implicit useNuxtApp() calls. Function signatures and plugin setups are updated to propagate the Nuxt app context explicitly throughout i18n locale detection, redirection, and SSR logic.

Changes

File(s) Change Summary
src/runtime/internal.ts Updated detectBrowserLanguage to accept nuxtApp as a parameter; removed internal useNuxtApp() usage.
src/runtime/utils.ts Refactored loadAndSetLocale, detectLocale, and detectRedirect to require explicit nuxtApp parameter.
src/runtime/plugins/i18n.ts Updated plugin to pass nuxtApp explicitly to i18n utility functions and retrieve app instance with ID.
src/runtime/plugins/route-locale-detect.ts Modified plugin setup to accept _nuxt, use its ID for app instance, and pass nuxtApp to utilities.
src/runtime/plugins/ssg-detect.ts Changed setup to accept _nuxt, use its ID for app instance, and pass nuxtApp to detectBrowserLanguage.
src/runtime/plugins/switch-locale-path-ssr.ts Changed setup to accept _nuxt, use its ID for app instance; logic otherwise unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant Plugin as Nuxt Plugin
    participant NuxtApp as NuxtApp Instance
    participant Util as i18n Utility Function

    Plugin->>NuxtApp: useNuxtApp(_nuxt._id)
    Plugin->>Util: call Utility Function (nuxtApp, ...)
    Util->>NuxtApp: Access i18n config/state via nuxtApp
    Util-->>Plugin: Return result
Loading

Possibly related PRs

Poem

🐇
With every hop, the context’s clear,
No more globals hiding here!
NuxtApp passed from paw to paw,
Locale detection free of flaw.
Refactored code, so neat and bright—
The i18n rabbit sleeps well tonight!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e3fbaa3 and 1ab6c34.

📒 Files selected for processing (6)
  • src/runtime/internal.ts (2 hunks)
  • src/runtime/plugins/i18n.ts (3 hunks)
  • src/runtime/plugins/route-locale-detect.ts (3 hunks)
  • src/runtime/plugins/ssg-detect.ts (2 hunks)
  • src/runtime/plugins/switch-locale-path-ssr.ts (1 hunks)
  • src/runtime/utils.ts (4 hunks)
🧰 Additional context used
🧠 Learnings (2)
src/runtime/plugins/i18n.ts (2)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
src/runtime/utils.ts (2)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
🔇 Additional comments (19)
src/runtime/plugins/switch-locale-path-ssr.ts (1)

9-11: LGTM! Consistent with the refactoring pattern.

The plugin correctly adopts the new explicit NuxtApp dependency pattern. The TypeScript expect-error comment appropriately handles the untyped internal _id parameter.

src/runtime/internal.ts (3)

2-2: Import cleanup is correct.

Removing useNuxtApp from imports since the function no longer uses it internally.


113-117: Excellent architectural improvement.

The function signature change to explicitly accept nuxtApp: NuxtApp removes implicit dependencies and makes the function more testable and explicit about its requirements.


126-127: Proper usage of explicit NuxtApp parameter.

The function now correctly uses the passed nuxtApp parameter instead of calling useNuxtApp() internally, which aligns with the architectural goals of this refactor.

src/runtime/plugins/i18n.ts (3)

39-40: Consistent refactoring pattern applied.

The plugin correctly adopts the explicit NuxtApp dependency pattern with appropriate TypeScript expect-error handling.


114-114: Function call updated correctly.

The loadAndSetLocale call now properly passes the nuxt instance as the first parameter, matching the updated function signature.


124-124: DetectRedirect call updated properly.

The function call correctly includes nuxtApp: nuxt in the options object, aligning with the refactored function signature.

src/runtime/plugins/ssg-detect.ts (2)

11-13: Plugin setup updated correctly.

The SSG detect plugin properly implements the explicit NuxtApp dependency pattern with appropriate TypeScript handling.


22-22: Function call correctly updated.

The detectBrowserLanguage call now properly passes nuxt as the first parameter, matching the updated function signature in src/runtime/internal.ts.

src/runtime/plugins/route-locale-detect.ts (4)

13-15: Setup function refactored correctly.

The route locale detect plugin properly adopts the explicit NuxtApp dependency pattern.


21-21: DetectLocale call updated properly.

The function call now correctly passes nuxt as the first parameter, aligning with the refactored utility function signatures.


35-35: LoadAndSetLocale call updated correctly.

The function call properly passes nuxt as the first parameter, matching the updated function signature.


56-56: DetectRedirect options updated appropriately.

The function call correctly includes nuxtApp: nuxt in the options object, consistent with the refactored function signature.

src/runtime/utils.ts (6)

53-57: Excellent refactoring to make dependencies explicit.

The addition of nuxtApp: NuxtApp as the first parameter removes the implicit dependency on useNuxtApp() within the utility function, improving testability and following dependency injection principles.


60-60: Proper usage of the injected dependency.

Correctly uses the passed nuxtApp parameter to access the i18n configuration instead of relying on implicit global state.


129-130: Consistent signature update for explicit dependency injection.

The function signature change aligns with the overall refactoring pattern and makes the NuxtApp dependency explicit.


140-140: Proper propagation of the NuxtApp instance.

Correctly passes the nuxtApp parameter to detectBrowserLanguage, maintaining consistency with the explicit dependency pattern.


172-173: Type definition updated to support explicit dependency injection.

The addition of nuxtApp: NuxtApp to the options type properly reflects the new function signature requirements.


191-196: Function signature and implementation properly updated.

The destructuring of nuxtApp from the options parameter and its usage on line 196 correctly replaces the implicit useNuxtApp() dependency with explicit parameter passing.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 12, 2025

npm i https://pkg.pr.new/@nuxtjs/i18n@3690

commit: 1ab6c34

@BobbieGoede
Copy link
Member Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 25, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@BobbieGoede BobbieGoede changed the title fix: nuxt app usage fix: reuse nuxt instance Jun 25, 2025
@BobbieGoede BobbieGoede changed the title fix: reuse nuxt instance fix: reuse nuxt instance at plugin setup Jun 25, 2025
@BobbieGoede BobbieGoede merged commit 0067f57 into v9 Jun 25, 2025
15 checks passed
@BobbieGoede BobbieGoede deleted the fix/nuxt-app-usage branch June 25, 2025 13:12
tobiasdiez pushed a commit to nuxt-modules/storybook that referenced this pull request Jun 26, 2025
<!--
☝️ PR title should follow conventional commits
(https://conventionalcommits.org).
In particular, the title should start with one of the following types:

- docs: 📖 Documentation (updates to the documentation or readme)
- fix: 🐞 Bug fix (a non-breaking change that fixes an issue)
- feat: ✨ New feature/enhancement (a non-breaking change that adds
functionality or improves existing one)
- feat!/fix!: ⚠️ Breaking change (fix or feature that would cause
existing functionality to change)
- chore: 🧹 Chore (updates to the build process or auxiliary tools and
libraries)
-->

### 🔗 Linked issue
* #899 
<!-- If it resolves an open issue, please link the issue here. For
example "Resolves #123" -->

### 📚 Description
Resolves #899 

Updates nuxt-i18n to a compatible version and updates the i18n
components in playground and showcase to use types and functions as
recommended by the docs.

#899 is actually resolved by
nuxt-modules/i18n#3690, though instances will
still get mixed up across stories (unrelated to nuxt-i18n).

<!-- Describe your changes in detail -->
<!-- Why is this change required? What problem does it solve? -->
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.

1 participant