Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(nuxt): populate route.meta.layout from appLayout route rules#34342

Open
M-YasirGhaffar wants to merge 1 commit intonuxt:mainfrom
M-YasirGhaffar:fix/route-meta-layout-appLayout-34305
Open

fix(nuxt): populate route.meta.layout from appLayout route rules#34342
M-YasirGhaffar wants to merge 1 commit intonuxt:mainfrom
M-YasirGhaffar:fix/route-meta-layout-appLayout-34305

Conversation

@M-YasirGhaffar
Copy link
Contributor

πŸ”— Linked issue

resolves #34305

πŸ“š Description

route.meta.layout is undefined when using appLayout in route rules, even though the layout renders correctly. The layout was only being resolved inside NuxtLayout but never written back to route.meta.layout.

This sets to.meta.layout from the route rule's appLayout in the router beforeEach guard, same spot and pattern used for appMiddleware. It only applies when to.meta.layout === undefined so it won't override definePageMeta or layout: false.

Also added a test to check route.meta.layout is actually set on the route-rules layout page.

@bolt-new-by-stackblitz
Copy link

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 17, 2026

Open in StackBlitz

@nuxt/kit

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

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@34342

nuxt

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

@nuxt/rspack-builder

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

@nuxt/schema

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

@nuxt/vite-builder

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

@nuxt/webpack-builder

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

commit: 9ed0934

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 17, 2026

Walkthrough

The pull request modifies the router setup to automatically populate route.meta.layout when not explicitly defined. When a route resolves and its layout metadata is undefined, the router now derives the layout from route rules based on the current path and assigns it to the route's meta object. The test suite is updated to validate this metadata assignment, and a test fixture page now displays the layout metadata to verify correct population. This ensures consistent layout metadata behaviour regardless of whether the layout is defined through page meta or route rules.

πŸš₯ Pre-merge checks | βœ… 6
βœ… Passed checks (6 passed)
Check name Status Explanation
Title check βœ… Passed The title accurately and concisely describes the main change: populating route.meta.layout from appLayout route rules.
Description check βœ… Passed The description clearly explains the issue, the fix applied, and references the linked issue, relating directly to the changeset.
Linked Issues check βœ… Passed The PR directly addresses issue #34305 by implementing the required fix to populate route.meta.layout from appLayout in route rules, matching the stated objectives.
Out of Scope Changes check βœ… Passed All changes (router logic, test assertion, and test fixture) are directly scoped to fixing the route.meta.layout population from appLayout route rules.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection βœ… Passed βœ… No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

🧹 Nitpick comments (1)
packages/nuxt/src/pages/runtime/plugins/router.ts (1)

185-190: Logic looks correct; consider reusing getRouteRules result to avoid a duplicate call.

getRouteRules({ path: to.path }) is called here and again at line 204 with the same argument within the same beforeEach guard. You could hoist the call and reuse routeRules for both the layout assignment and the middleware lookup.

♻️ Suggested refactor
+      const routeRules = getRouteRules({ path: to.path })
       if (to.meta.layout === undefined) {
-        const appLayout = getRouteRules({ path: to.path }).appLayout
+        const appLayout = routeRules.appLayout
         if (appLayout) {
           to.meta.layout = appLayout as Exclude<PageMeta['layout'], Ref | false>
         }
       }
       nuxtApp._processingMiddleware = true

       if (import.meta.client || !nuxtApp.ssrContext?.islandContext) {
         type MiddlewareDef = string | RouteMiddleware
         const middlewareEntries = new Set<MiddlewareDef>([...globalMiddleware, ...nuxtApp._middleware.global])
         for (const component of to.matched) {
           const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[]
           if (!componentMiddleware) { continue }
           for (const entry of toArray(componentMiddleware)) {
             middlewareEntries.add(entry)
           }
         }

-        const routeRules = getRouteRules({ path: to.path })
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nuxt/src/pages/runtime/plugins/router.ts` around lines 185 - 190,
The code calls getRouteRules({ path: to.path }) twice inside the same beforeEach
guard; hoist this call into a local const (e.g., routeRules = getRouteRules({
path: to.path })) and reuse routeRules for both the layout assignment
(to.meta.layout) and the later middleware lookup, replacing the duplicate calls
to getRouteRules to avoid redundant computation.
πŸ€– Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/nuxt/src/pages/runtime/plugins/router.ts`:
- Around line 185-190: The code calls getRouteRules({ path: to.path }) twice
inside the same beforeEach guard; hoist this call into a local const (e.g.,
routeRules = getRouteRules({ path: to.path })) and reuse routeRules for both the
layout assignment (to.meta.layout) and the later middleware lookup, replacing
the duplicate calls to getRouteRules to avoid redundant computation.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 17, 2026

Merging this PR will not alter performance

βœ… 21 untouched benchmarks


Comparing M-YasirGhaffar:fix/route-meta-layout-appLayout-34305 (9ed0934) with main (e16c835)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (fdd3034) during the generation of this report, so e16c835 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

if (to.meta.layout === undefined) {
const appLayout = getRouteRules({ path: to.path }).appLayout
if (appLayout) {
to.meta.layout = appLayout as Exclude<PageMeta['layout'], Ref | false>
Copy link
Member

Choose a reason for hiding this comment

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

I have an open question in my mind as to whether this is correct, because appLayout is being applied by Nitro route rules, which do not necessarily match the vue-router route. You might have a route, for example, like a catch-all, where the layout applies to only some of the paths that that route represents. It might not be correct to set layout metadata on the route itself.

I would also want to see what happens if we transition from one page rendered by this route which does match an app layout rule to another page which doesn't match the app layout rule. Does the metadata change? We probably need to make sure that it does.

Or maybe we need to rethink this PR. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see, yeah with a catch-all route the layout would leak on the shared meta object and not clear when navigating to a path without an appLayout rule. A reset of to.meta.layout to undefined at the start of each navigation (when not readonly) should handle that.

But if mutating route meta isn't the right call here, what approach would you go with instead?

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.

bug: route.meta.layout is undefined when using appLayout in Nuxt 4.3.1

2 participants