-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt3): add support for definePageMeta
macro
#2678
Conversation
βοΈ Deploy Preview for nuxt3-docs canceled. π¨ Explore the source changes: 1160770 π Inspect the deploy log: https://app.netlify.com/sites/nuxt3-docs/deploys/61e5b4fd68f37100081b6f14 |
* e.g. windows drive colon
I like it! For the record, there is https://github.com/dword-design/nuxt-route-meta which provides similar features (also for nuxt2). Inspired by this library, maybe its worthwhile to also support the following syntax export default defineNuxtComponent({
pageMeta: {
layout: 'custom',
auth: true
}
} to set the page metadata. What I'm missing for the docs is how this is used with typescript, i.e. how to define a custom PageMeta object to make sure that one only uses 'known' metadata objects. For this the current implementation export interface PageMeta {
[key: string]: any
...
} might lead to problems. For example, if I define |
In general Looks good to me π But some notes:
|
@tobiasdiez Good point! I might be wrong but I guess when extending interface with types, typescript checks it over generic interface PageMeta {
[key: string]: any
}
interface CustomPageMeta extends PageMeta {
custom: number
}
// Type 'string' is not assignable to type 'number'.ts(2322)
const meta: CustomPageMeta = { custom: 'string' } But it might be also good idea if we use something like |
I agree that the more narrow types overwrite the generic keys. What I meant is that in your example
doesn't lead to a typescript error. Thus one cannot easily say "only the following keys are allowed" (or at least I don't know how to exclude the |
If we include a sub-namespace of |
ref: #2711, #2706
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Renovate Bot <bot@renovateapp.com>
A couple of comments - the macro plugin will allow us to define other macros, like By doing this, we allow Another benefit is that we can declare types for these keys on Personally, I think the user DX of accessing something from e.g. I'm happy to remove the layout implementation π However, I think we will still need something it even with |
Co-authored-by: pooya parsa <pyapar@gmail.com>
export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> = new Set()): { imports: Set<string>, routes: NuxtPage[]} { | ||
return { | ||
imports: metaImports, | ||
routes: routes.map((route) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can cause issues if people have components that don't export meta. Specifically, people will run into "The requested module ...' does not provide an export named 'meta'. That means every component will need to be updated to export const meta = {}
at the least, which might not be great DX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We provide a default meta export (equalling undefined) via the transform plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this PR should probably be revisited, as it introduced unintended effects, such as this one and failing to honor the layouts
property in the component. That layouts prop now needs to be moved into the meta section. If that's the design, then the docs should be updated to alert people (users of the Options API) to the breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are experiencing a problem (you mention unintended side effects), would you be able to raise an issue?
Equally, I'd welcome any thoughts on the docs: http://v3.nuxtjs.org/docs/directory-structure/layouts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, if I use the extendPages
method, even if I specify meta to {}
, I encounter this error:
""The requested module ...' does not provide an export named 'meta'"
My only way at the moment to get around the issue is to have the component export meta. I'd be happy to provide input on that layouts doc.
π Linked issue
https://github.com/nuxt/framework/discussions/2468
β Type of change
π Description
This PR is the first step in features enabled by 'extracting' metadata from components. I've included implementations of transition props and layouts via
definePageMeta
(though happy to extract the layouts change to another PR).NOTE: at the moment HMR isn't working on route meta.
Transition props
It is now possible to define transition props which will be applied to the
<transition>
wrapping the page component (or pass false to disable it entirely):framework/packages/nuxt3/src/pages/runtime/page.vue
Lines 4 to 8 in 07df171
Layouts
This PR changes how layouts are defined. Previously they had to be defined via a component option on the top-level page (ie. not in a child route). Now, they can be defined in a parent or nested route, via
definePageMeta({ layout })
. This also now accepts a computed property or a ref (for reactive layout changes within a page).Middleware
I will be following up shortly with a middleware implementation.
Route key
I will be following up shortly with a route key implementation.
π Checklist