<Layout>
Environment: client, and server if ssr: true
.
Implemented by: vike-react
/vike-vue
/vike-solid
.
You need
vike-react
/vike-vue
/vike-solid
to be able to use theLayout
setting. If you don't usevike-react
/vike-vue
/vike-solid
then see Withoutvike-{react,vue,solid}
.
What are layouts?
Your pages will usually share a common visual design.
Some pages may share a design while other pages share another design. (For example, two admin pages
/admin/product/@id
and/admin/user/@id
sharing the same navigation sidebar, and two marketing pages/
and/about-us
not having any sidebar but sharing a sticky header instead.)The
Layout
setting enables you to define such shared visual appearance.
The component defined by the Layout
setting wraps the <Page>
component.
For integrating tools, we generally recommend using
<Wrapper>
instead.
Global layout
You can define a layout that applies to all your pages:
Why
/pages/+Layout.jsx
applies to all pages is explained at API > Config Files > Inheritance.
Multiple layouts
You can define several layouts that apply to different groups of pages.
The directory
(marketing)
is skipped by Filesystem Routing, see Guides > Routing > Filesystem Routing.
Setting a default
<Layout>
that is overridable isn't currently supported, see #1692 - Addoverride
anddefault
options for cumulative configs.
Nested layouts
You can define layouts that nest into each other:
Here pages/+Layout.js
applies to all pages, including the marketing and admin pages.
The
Layout
setting is cumulative:pages/(marketing)/+Layout.js
doesn't overridepages/+Layout.js
. Instead, the<Layout>
components nest within each other:
You can also implement same-page navigations such as tabs:
/product/42/pricing /product/42/reviews
+------------------+ +-----------------+
| Macbook | | Macbook |
| ... | | ... |
| +--------------+ | | +-------------+ |
| | Pricing | | +------------> | | Reviews | |
| | ... | | | | ... | |
| +--------------+ | | +-------------+ |
+------------------+ +-----------------+
If your nested layout isn't associated with a URL (if the pricing and reviews tabs share the same URL
/product/42
) then you can use a stateful component instead of<Layout>
.
To avoid the page scrolling to the top, make sure to use:
Examples:
Data fetching
To fetch data for your layouts see:
Without vike-{react,vue,solid}
The following is for users that don't use a UI framework Vike extension vike-react
/vike-vue
/vike-solid
.
The simple way
A simple way to implement layouts is to manually wrap your <Page>
components:
With a custom setting
You can implement the Layout
setting yourself by using meta.
Examples:
Nested Layout
See the Nested layouts section above. For smooth nested layout navigation, we recommend using Client Routing. (Using Server Routing leads to full page reloads which usually isn't acceptable for same-page navigations.)