Skip to content

refactor(nuxt): define layouts as async vue components#29957

Merged
danielroe merged 1 commit intomainfrom
fix/layout-async-components
Nov 17, 2024
Merged

refactor(nuxt): define layouts as async vue components#29957
danielroe merged 1 commit intomainfrom
fix/layout-async-components

Conversation

@danielroe
Copy link
Member

@danielroe danielroe commented Nov 17, 2024

🔗 Linked issue

📚 Description

this follows up on vuejs/core#6638 to mark layouts as explicit vue components

it should also be slightly more performant 🙏

Summary by CodeRabbit

  • New Features

    • Simplified layout loading process by switching from asynchronous to synchronous calls, which may enhance performance.
    • Enhanced functionality of layout and middleware templates for better component and middleware handling.
  • Bug Fixes

    • Maintained error handling for invalid layouts with console warnings.
  • Documentation

    • Updated comments and added TODOs for future improvements in the codebase.

@bolt-new-by-stackblitz
Copy link

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 17, 2024

Walkthrough

The pull request modifies the LayoutLoader component in nuxt-layout.ts by changing the layout component loading from asynchronous to synchronous. This simplification removes the need for promise handling in the setup function. Additionally, updates to templates.ts enhance the structure of layoutTemplate and middlewareTemplate, allowing for better management of component definitions and middleware exports. The overall functionality and error handling remain consistent, ensuring that the components operate correctly despite the changes in loading strategies.

Changes

File Path Change Summary
packages/nuxt/src/app/components/nuxt-layout.ts Changed async setup to setup in LayoutLoader, removing asynchronous loading of layouts.
packages/nuxt/src/core/templates.ts Updated layoutTemplate to use defineAsyncComponent for layout definitions; refined middlewareTemplate for better export handling. Adjusted getContents method in both templates.

Possibly related PRs

  • refactor(nuxt): delete redundant type #29810: The changes in this PR involve modifications to the getContents method, which may relate to how layout components are defined and managed, similar to the changes in the LayoutLoader component's setup function in the main PR.

Suggested labels

refactor

Poem

In the land of code where rabbits play,
Layouts now load in a simpler way.
No more waiting, just hop right in,
With templates refined, let the fun begin!
🐰✨

Warning

Rate limit exceeded

@danielroe has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 4 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 4924e26 and f5c9fb6.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
packages/nuxt/src/app/components/nuxt-layout.ts (1)

26-29: LGTM! Performance improvement through sync component loading.

The change from async to sync layout loading is a good optimization. By moving the async handling to build time, we reduce runtime overhead and improve performance, which aligns with the PR objectives.

This architectural change follows Vue.js best practices by pre-compiling components and avoiding unnecessary async operations at runtime.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4924e26 and f5c9fb6.

📒 Files selected for processing (2)
  • packages/nuxt/src/app/components/nuxt-layout.ts (1 hunks)
  • packages/nuxt/src/core/templates.ts (1 hunks)
🔇 Additional comments (2)
packages/nuxt/src/app/components/nuxt-layout.ts (1)

26-29: Verify build-time layout compilation.

Since we're now assuming synchronous access to layouts, we should verify that all layouts are properly pre-compiled during the build phase.

✅ Verification successful

Based on the examination of the codebase, I can now generate a final response regarding the layout compilation verification.

Layout compilation is properly handled at build time.

The codebase shows that layouts are:

  • Imported synchronously from a virtual module #build/layouts (as seen in nuxt-layout.ts)
  • Registered during app initialization via the addLayout function in packages/kit/src/layout.ts
  • Compiled and added to the app templates through the app:templates hook
  • Accessed synchronously in the LayoutLoader component without any dynamic imports
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that layouts are properly compiled as Vue components

# Check the layout template generation
ast-grep --pattern 'export const layoutTemplate = $layout'

# Check for any remaining async imports in layouts
rg -l "import.*async.*from.*layouts" 

# Look for any dynamic import() calls for layouts
rg "import\(.*layout.*\)"

Length of output: 127


Script:

#!/bin/bash
# Let's try a different approach to verify layout compilation

# Check how layouts are registered/handled in the codebase
rg -A 5 "layouts\s*=\s*{" 

# Look for layout-related build/compilation logic
rg -l "layout.*build|build.*layout"

# Check for any layout registration or compilation utilities
ast-grep --pattern 'function $name($params) {
  $$$
  layout
  $$$
}'

# Look for layout-related plugins or modules
fd "layout" --type f

Length of output: 1148


Script:

#!/bin/bash
# Let's examine the layout compilation and registration process

# Check the content of layout-related files
cat packages/kit/src/layout.ts

# Look for layout registration in nuxt-layout component
cat packages/nuxt/src/app/components/nuxt-layout.ts

# Check the layout plugin
cat packages/nuxt/src/app/plugins/check-if-layout-used.ts

# Search for layout compilation/build references with more context
rg -A 5 "defineNuxtConfig|buildModule.*layout"

Length of output: 77379

packages/nuxt/src/core/templates.ts (1)

288-288: LGTM! Well-structured implementation of async Vue components

The changes properly implement async Vue components by:

  1. Importing defineAsyncComponent from Vue
  2. Wrapping layout components with defineAsyncComponent

This refactor aligns with Vue 3 best practices and should provide better type safety and runtime performance.

Also applies to: 291-291

const LayoutComponent = await layouts[props.name]().then((r: any) => r.default || r)

return () => h(LayoutComponent, props.layoutProps, context.slots)
return () => h(layouts[props.name], props.layoutProps, context.slots)
Copy link
Contributor

Choose a reason for hiding this comment

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

@danielroe is there any chance that the removal of await before calling layouts[props.name] here (or another change in this PR) could cause a hydration error in some scenarios?

After upgrading to v3.14.1592 (the only change in my project) I'm now seeing Hydration completed but contains mismatches on a page that only contains a NuxtLayout and a paragraph tag in the default slot.

hydration error

It doesn't occur when the dev server is running; only when building and running the build (e.g. with wrangler).

I've tried putting together a reproduction but (of course) it doesn't happen there as of yet.

Do you have any suggestions on how to further triage? Typically I'd start disabling modules, etc. but in a larger project, that isn't the easiest task since so many things depend on others 😬

Copy link
Contributor

Choose a reason for hiding this comment

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

It actually also seems to break styles injected during SSR via useHead 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Put together an issue; I'm not confident this PR is the root of the issue, but it's my best guess lol
#30188

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.

2 participants