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

avoid code duplication in page components #291

Open
fago opened this issue Jan 12, 2025 · 3 comments
Open

avoid code duplication in page components #291

fago opened this issue Jan 12, 2025 · 3 comments

Comments

@fago
Copy link
Contributor

fago commented Jan 12, 2025

When adding multiple page components, as done since #290, there are quite some duplicate code parts in the setup helper. Let's provide some composables to void this code duplication.

@fago
Copy link
Contributor Author

fago commented Jan 15, 2025

Suggestion:

  1. Move layout handling out from page component to layout.vue - so it's centralized and we avoid duplication. Usually all pages have the same layout. That's a good default and if it's not fitting, projects are free to move it over / back to pages.

app.vue

<template>
 <NuxtLayout :name="useDrupalCe().getPageLayout()">
   <NuxtPage />
 </NuxtLayout>
</template>

We need to make the page parameter to getPageLayout optional and default to useDrupalCe().getPage() if not given to make it this short.

  1. Simplify page component code to this, by introducing a new helper: usePageHead()
<template>
  <main>
    <SiteBreadcrumbs />
    <DrupalTabs v-if="page.local_tasks" :tabs="page.local_tasks" />
    <component :is="renderCustomElements(page.content)" />
  </main>
</template>

<script lang="ts" setup>
const { fetchPage, renderCustomElements, usePageHead } = useDrupalCe()
const page = await fetchPage(useRoute().path, { query: useRoute().query })
usePageHead(page)
</script>

@fago
Copy link
Contributor Author

fago commented Jan 15, 2025

layout handling has test-coverage already, but metadata handling has not. thus added it: #295

@vloss3
Copy link
Contributor

vloss3 commented Jan 15, 2025

  1. the fetchPage request to find layout happens after app.vue is rendered so I don't think it would work, but agree that NuxtLayout in app.vue is unnecessary, we can keep it in pages only
  2. looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants