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

perf(nuxt): enable Transition component only on client side #30720

Merged
merged 8 commits into from
Jan 24, 2025

Conversation

Mini-ghost
Copy link
Member

@Mini-ghost Mini-ghost commented Jan 23, 2025

🔗 Linked issue

📚 Description

  • Referenced the approach used in wrapInKeepAlive, ensuring that the <Transition> components inside <NuxtPage> and <NuxtLayout> are initialized only on the client side.
  • Separated the code executed on the server from the code executed on the client, which may improve clarity when reading.

Copy link

stackblitz bot commented Jan 23, 2025

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

Copy link

pkg-pr-new bot commented Jan 23, 2025

Open in Stackblitz

@nuxt/kit

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

@nuxt/rspack-builder

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

nuxt

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

@nuxt/schema

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

@nuxt/vite-builder

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

@nuxt/webpack-builder

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

commit: 187cdeb

Copy link

codspeed-hq bot commented Jan 23, 2025

CodSpeed Performance Report

Merging #30720 will not alter performance

Comparing Mini-ghost:refactor/server-side-transition (187cdeb) with main (7eeb910)

Summary

✅ 8 untouched benchmarks

@Mini-ghost Mini-ghost marked this pull request as ready for review January 24, 2025 10:42
@Mini-ghost Mini-ghost requested a review from danielroe as a code owner January 24, 2025 10:42
Copy link

coderabbitai bot commented Jan 24, 2025

Pull Request Changes Analysis

Walkthrough

The pull request introduces significant changes to Nuxt's component rendering and transition handling. The modifications primarily focus on the nuxt-layout.ts, utils.ts, and page.ts files within the Nuxt package. The core change involves replacing the _wrapIf utility function with a new _wrapInTransition function, which alters how components are wrapped and transitions are applied.

The changes enhance the component rendering process, particularly for server-side rendering scenarios. A new approach to handling transitions has been implemented, with modifications to import statements and the way components are wrapped. The NuxtPage component now includes improved logic for managing asynchronous components and transitions, with a particular focus on creating a more robust rendering mechanism.

Additionally, a minor adjustment was made to a bundle size test, reflecting the impact of these architectural changes on the overall bundle size.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

@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/pages/runtime/utils.ts (1)

28-30: Consider adding type definitions for clarity

The wrapInTransition function currently uses any for the props and children parameters. Defining explicit types for them would improve code reliability and maintainability, reducing uncertainty for future readers.

-export const wrapInTransition = (props: any, children: any) => {
+interface TransitionProps {
+  // Define specific transition props here (e.g. name?: string)
+}
+
+interface TransitionChildren {
+  default?: () => VNode | VNode[]
+  // Add other slot definitions as needed
+}
+
+export const wrapInTransition = (props: TransitionProps | boolean, children: TransitionChildren) => {
   return {
     default: () => import.meta.client && props
       ? h(Transition, props === true ? {} : props, children)
       : children.default?.()
   }
 }
packages/nuxt/src/pages/runtime/page.ts (2)

106-123: Consider adding error boundaries for SSR <Suspense>

The server-side <Suspense> usage is sound. For resilience, handle error states or a fallback to ensure graceful failure in SSR.


158-158: Avoid mutating (providerVNode.type as any).name

Altering the VNode type's name property can cause confusion in devtools and may break assumptions about the component. Prefer naming patterns that do not rely on direct mutation.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8e462d4 and 025fb97.

📒 Files selected for processing (5)
  • packages/nuxt/src/app/components/nuxt-layout.ts (2 hunks)
  • packages/nuxt/src/app/components/utils.ts (1 hunks)
  • packages/nuxt/src/pages/runtime/page.ts (4 hunks)
  • packages/nuxt/src/pages/runtime/utils.ts (2 hunks)
  • test/bundle.test.ts (1 hunks)
🔇 Additional comments (11)
packages/nuxt/src/app/components/nuxt-layout.ts (3)

2-2: Verify the necessity of importing Suspense

Suspense is imported here. Confirm that it is indeed required in this file for the intended usage. If it is no longer needed, removing the import would lighten the code.


9-9: Replacement of _wrapIf with wrapInTransition

The switch to wrapInTransition aligns with the PR’s objective to enable <Transition> on the client side. The import is straightforward and appears correct.


83-83: Assess transition fallback logic

When hasLayout && transitionProps is falsy, no transition is used. Confirm that omitting transitions in that scenario is the intended behaviour and does not adversely affect SSR or client rendering.

packages/nuxt/src/app/components/utils.ts (1)

2-2: Unused import removed

Removing Component from the imports aligns with the removal of _wrapIf. This update helps keep the file lean and avoids importing unused types.

test/bundle.test.ts (1)

130-130: Update to bundle size snapshot

The expected server bundle size changed from 303k to 301k. This suggests a slight reduction, which is beneficial. Keep the snapshot in sync if there are additional changes to the bundle size.

packages/nuxt/src/pages/runtime/page.ts (6)

1-1: No issues with updated imports

The newly added imports from 'vue' align well with the introduced logic.


7-7: Newly imported wrapInTransition aligns with objectives

Introducing wrapInTransition here is consistent with the PR's goal of refining transition handling in the codebase.


103-103: Confirm concurrency handling for previousPageKey

Assigning previousPageKey = key helps track route changes, but verify that no race conditions arise during concurrent or rapid navigations.


125-125: Client-side rendering comment

The comment adequately clarifies the client-side logic. No additional concerns.


Line range hint 126-135: Transition and keep-alive logic looks correct

Combining merged transition properties with keep-alive ensures consistent behaviour between user-defined and default transitions.


Line range hint 136-157: Proper client-side wrapInTransition usage

Executing wrapInTransition and wrapInKeepAlive on the client side is a clear approach for conditionally running transitions. Nicely structured.

@danielroe danielroe changed the title refactor(nuxt): enable Transition component only on client side perf(nuxt): enable Transition component only on client side Jan 24, 2025
previousPageKey = key

if (import.meta.server) {
Copy link
Member

Choose a reason for hiding this comment

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

looking at the vue implementation of Transition for SSR, is there any performance benefit or reason to have a separate implementation in nuxt? (I'm guessing yes, but just want to confirm.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I looked into the implementations of <KeepAlive> and <Transition>.

It seems that <KeepAlive> in SSR scenarios returns the default slots early on the server side without continuing to execute its internal implementation.

https://github.com/vuejs/core/blob/29216853d4eb2bc72c284bcc5234afbca9c81854/packages/runtime-core/src/components/KeepAlive.ts#L79-L109

For <Transition>, I couldn’t find any related SSR-specific implementations in BaseTransition.

https://github.com/vuejs/core/blob/29216853d4eb2bc72c284bcc5234afbca9c81854/packages/runtime-core/src/components/BaseTransition.ts#L145-L267

The reason for opening this PR is that <Transition> involves DOM operations, so even if the component is initialized on the server side, its functionality wouldn’t be utilized. Additionally, <Transition> does not generate extra DOM nodes, so skipping its execution on the server side will not cause hydration errors on the client side. Based on these considerations, I’ve made adjustments in this PR.

Separating the server-side and client-side implementations also helps to clearly show the differences between the two, rather than relying on numerous conditional checks to understand the logic.

If there are any adjustments or considerations I might have missed, please let me know. I truly appreciate your feedback. Thank you very much!

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/pages/runtime/page.ts (2)

137-137: Refactor nested logic for readability

Conditionally wrapping everything with _wrapInTransition in the same statement could reduce readability over time. Consider extracting the keep-alive or transition arguments into local variables for cleaner code.


159-159: Avoid altering the component’s type name inline

Modifying (providerVNode.type as any).name can confuse debugging and tool integrations. It may be clearer to pass a name via component options or refactor your approach to avoid direct reassignment.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 025fb97 and 187cdeb.

📒 Files selected for processing (3)
  • packages/nuxt/src/app/components/nuxt-layout.ts (2 hunks)
  • packages/nuxt/src/app/components/utils.ts (2 hunks)
  • packages/nuxt/src/pages/runtime/page.ts (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nuxt/src/app/components/nuxt-layout.ts
🔇 Additional comments (5)
packages/nuxt/src/pages/runtime/page.ts (3)

1-1: No concerns with the updated imports

These newly added imports are appropriate for advanced component rendering in Vue and fully align with the new SSR approach.


12-12: Consistent switch to _wrapInTransition

Replacing _wrapIf with _wrapInTransition at import time is coherent with the new transition usage. Please ensure any remaining references to _wrapIf are removed throughout the codebase.


107-125: Consider adding SSR fallback tests

You have introduced a Suspense-based approach for server-side rendering. While this looks correct, please ensure that fallback scenarios for errors and timeouts are thoroughly tested to avoid hydration issues.

packages/nuxt/src/app/components/utils.ts (2)

1-2: Imports look good

These new imports for Transition, createStaticVNode, and h match their usage in this module. No compatibility concerns observed.


13-13: Handle edge cases in _wrapInTransition

This new function correctly ensures transitions only run on the client. However, verify that when children.default is undefined or false, the code gracefully returns without error.

@danielroe danielroe merged commit 8866631 into nuxt:main Jan 24, 2025
48 checks passed
@github-actions github-actions bot mentioned this pull request Jan 24, 2025
@github-actions github-actions bot mentioned this pull request Jan 24, 2025
@Mini-ghost Mini-ghost deleted the refactor/server-side-transition branch January 25, 2025 17:57
Kamsou pushed a commit to Kamsou/nuxt that referenced this pull request Feb 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.

2 participants