-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Discussion] Is there a better way to use layouts with current LayoutDistributor
#878
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
Comments
Besides the already mentioned usage of
Currently, VuePress seems to behave very odd in these cases, since the 404s content can already be specified using a 404.md file (however, its content is sadly not leveraged for actual 404 cases to my knowledge, but fully replaced by the layout vuepress/packages/@vuepress/core/lib/build.js Lines 73 to 76 in 6911756
However, the current implementation also forces the user to use another (or duplicated) layout by requiring Given the current option to specify a new layout on demand, It would seem to be more expected and flexible (in case no additional layout is wanted), to simply provide a Users with multiple sites and a common layout could greatly benefit from this separation of content and layout (as we already do for all other kind of pages), as the 404s content can freely be chosen for each individual page (for example to display different, useful links for a lost customer), instead of building a workaround by shifting all the content (e.g. as objects or HTML strings) into the For example (based on the default 404): 404.md ---
title: 404 - Nothing to see here
layout: NotFound
---
# 404
> {{ getMsg() }}
[Take me home.](/)
<script>
const msgs = [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
]
export default {
methods: {
getMsg () {
return msgs[Math.floor(Math.random() * msgs.length)]
}
}
}
</script> and NotFound.vue <template>
<div class="theme-container">
<Content></Content>
</div>
</template>
<script>
export default {}
</script> |
A quick fix to achieve the latter (to normalise the 404 handling) could be to change vuepress/packages/@vuepress/core/lib/prepare/ClientComputedMixin.js Lines 18 to 21 in 8003425
into return pages.find(page => page.path.toLowerCase() === '/404.html'); And
could be simplified to (as I see no other reason for return this.$page.frontmatter.layout || 'Layout' Given that the vuepress/packages/@vuepress/core/lib/build.js Lines 73 to 76 in 6911756
, it is safe to assume that the However, it would add the breaking change that -- compared to now -- the |
Agreed, replacing the entire Layout feels weird to me as I'm used to have at least one common component to work with. As a workaround I just don't use ---
title: Home
use: HomeLayout
--- Then I have complete control over my layout distribution (even for pages that did not provide the option in the frontmatter). computed: {
layout () {
if (this.$page.regularPath.startsWith('/articles')) {
return 'ArticleLayout'
}
return this.$page.frontmatter.use || 'PageLayout'
}
} |
After 1.0.0-alpha.32, if the layout in frontmatter does not exist, it will fallback to So another workaround is to have only one |
Isn’t that the same as my previous comment? |
@lorisleiva Similar but not the same, as you can use If you are writing a theme which allow users to control the layout of some page, it's better not to introduce extra keyword for that. If not, they are the same indeed. |
Oh great! Sorry I didn’t get that from your previous message. |
Support configure |
Version 1.0.0-alpha.1
Sorry for not obeying the issue template, but I just want to discuss the
LayoutDistributor
that introduced inv1.0.0-alpha
, which seems like to be one of the major breaking changes.This component is not complicated. What it does is changing the "root" component according to the
layout
option in frontmatter:vuepress/packages/@vuepress/core/lib/app/components/LayoutDistributor.vue
Lines 1 to 16 in ace3aec
When switching between pages with different layouts, the "root" component will be replaced by another one, i.e. current layout will be replaced totally by another layout.
However, different layouts may extend from a same parent-layout, so they have some parts in common, which should not be replaced "totally".
In a common scenario, one may want to add
transition
when switching between layouts, but the "root" component is replaced so notransition
could be added.In addition,
LayoutDistributor
also changed the way how default theme handle custom layouts. Following code is buggy / has no effect when working together withLayoutDistributor
.vuepress/packages/@vuepress/theme-default/layouts/Layout.vue
Lines 32 to 37 in ace3aec
In my opinion:
LayoutDistributor
could help them to set layouts without extra code. But the "old way" is more flexible and extendable, which may be preferred by theme makers.The text was updated successfully, but these errors were encountered: