-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
Feature: Dynamic route parameters should not be set using definePageMeta #1736
Comments
This issue was miss-taken. |
I have the same issue. My dirty workaround for the time being: <script setup>
// /pages/[slug].vue
const route = useRoute()
const { locale } = useI18n()
const { data } = await useFetch(
`https://yourapi.com/${locale}/page/${route.params.slug}`
)
if (data?.value) {
route.meta.nuxtI18n = data.value.other_languages;
}
</script> |
having the same trouble! in nuxt 2 I used this: https://i18n.nuxtjs.org/lang-switcher#dynamic-route-parameters |
Got is working like this: // pages/blog/[slug].vue
const { data: blog } = await useFetch(`https://yourapi.com/${locale}/page/${route.params.slug}`)
if (blog?.value) {
route.meta.nuxtI18n = blog.value?.languages || {};
}
const head = useLocaleHead({
identifierAttribute: "id",
addSeoAttributes: true,
});
useHead({
link: head.value.link,
meta: head.value.meta,
}); Where {
en: { slug: "english-slug" },
de: { slug: "german-slug" },
fr: { slug: "french-slug" }
} Works both in SSR and normal mode. |
It seems not to work with SSG. Has anyone tried to get with working with static rendering? |
@adriaanvanrossum I double checked. |
For me, it worked as well, until it didn't anymore :) |
The workaround seems to work, but it seems you will have to set the route meta on each route change. I made a reproduction to show that the meta links lose their translations if query parameters change and the route meta is not set again here. |
@BobbieGoede |
@provok-it I ended up with the following composable, it's not pretty but it seems to work so far. import { resolveUnref } from "@vueuse/shared";
type UseHeadParams = Parameters<typeof useHead>;
type useLocaleHeadParams = Parameters<typeof useLocaleHead>[0];
export default ([input, options]: UseHeadParams, localeOptions: useLocaleHeadParams) => {
const route = useRoute();
const i18nHead = useLocaleHead(localeOptions);
useHead(() => {
const i = resolveUnref(input);
return {
...i,
link: [...(resolveUnref(i?.link) ?? []), ...(i18nHead.value.link ?? [])],
meta: [...(resolveUnref(i?.meta) ?? []), ...(i18nHead.value.meta ?? [])],
};
}, options);
const _i18nParams = ref<Record<string, any>>({});
const i18nParams = computed({
get: () => _i18nParams.value,
set: (val: Record<string, any>) => {
_i18nParams.value = val;
route.meta.nuxtI18n = i18nParams.value;
},
});
watch(
() => route.fullPath,
() => (route.meta.nuxtI18n = i18nParams.value)
);
return { i18nParams };
}; Usage like so const { i18nParams } = useI18nParams(
[() => ({ title: t('productSearchTitle') })],
{ identifierAttribute: 'id', addSeoAttributes: true }
)
const fetchProducts = async () => {
// ... fetch products
// set i18nParams triggering composable to update `route.meta.nuxtI18n`
i18nParams.value = {
en: { slug: "english-slug" },
de: { slug: "german-slug" },
fr: { slug: "french-slug" }
}
} |
Can't get this to work with SRR. It always renders a diferent link between the view-source and the inspector. Even if I do something like this to try test it. It works but the view-source will have different links in the language switcher.
|
Try using |
I'm experiencing the same issue. The const { data } = await asyncData(`https://myapi.com/page`)
if (data?.value) {
route.meta.nuxtI18n = {
en: {
slug: data.value?.page?.languages.en.slug
}
}
} But the |
I'm experiencing the same issue: |
I changed Lang Switcher slightly on strategy 'prefix_except_default'
Hope that helps. |
This worked for me. I adjusted it a little bit to still be able to make use of <NuxtLink
v-for="locale in locales"
:key="locale.code"
:to="switchLocale(locale.code)"
>
{{ locale.name }}
</NuxtLink> const switchLocalePath = useSwitchLocalePath()
const switchLocale = (locale: string) => {
return switchLocalePath(locale)
} nonetheless – this is a workaround and only works if I hope we can find a solution for using definePageMeta({
nuxtI18n: data.value.other_languages // Throws an error because data is undefined
}) |
This solution route.meta.nuxtI18n works perfectly for me. But i have this kind of message: Please explain to me why I get this kind of message. |
Your router middleware's function has already access to the next and previous routes parameters since it handles a route change event. Accessing the current route data with useRoute makes no sense because you're in between routes. Use the "to" (first param of the middleware function) to access the current route data. ✌️ |
I also face this problem. I tried several workarounds, not being able to define it dynamically doesn’t make sense. Thanks for this great plugin in all the way! |
Any updates? The hacky workarounds mentioned here are somewhat working, but the issue with |
I gave this another try but still can't get dynamic route parameters to work properly with data that is being fetched async. As mentioned in the docs i tried to set them via the compiler macro In my case i fetch some data: <script lang="ts" setup>
const { locale } = useI18n()
const { data } = await useAsyncGql({
operation: 'getSlugPage',
variables: {
site: locale, // de
slug: 'ueber-uns',
},
})
// ... The data i recive includes a page with a translation:
Then i would like to use this data and set the dynamic routes on nuxtI18n with it: definePageMeta({
nuxtI18n: {
de: { slug: [data.value.page.slug] }, // should resolve to ueber-uns
en: { slug: [data.value.page.translation.slug] } // should resolve to about-us
},
}) When trying to do so i get the error that [nuxt] [request error] [unhandled] [500] data is not defined
at ./pages/[...slug].vue:2:21
at ViteNodeRunner.directRequest (./node_modules/vite-node/dist/client.mjs:341:11) If Many thanks for any help! |
Hi, you can add it in script blog. Try...
|
Thank you @bitmonti for the quick respond. I have tried your suggestion and it seems to work when wrapped inside <script setup>
onBeforeMount(() => {
route.meta.nuxtI18n =
{
de: { slug: [data.value.page.slug] },
en: { slug: [data.value.page.translation.slug] },
} || {} What it doesn't seem to fix is the problem when prerendering the page with Prerendering 8 initial routes with crawler
├─ /
├─ /en
├─ /en/about-us
├─ /about-us
├─ /ueber-uns
├─ /en/ueber-uns
... My langswitch component looks like this (similar to your approach): <template>
<div>
<NuxtLink
v-for="loc in availableLocales"
:key="loc.code"
:to="switchLocale(loc.code)"
>
{{ loc.name }}
</NuxtLink>
</div>
</template>
<script lang="ts" setup>
const switchLocalePath = useSwitchLocalePath()
const { locale, locales } = useI18n()
const availableLocales = computed(() =>
locales.value.filter((loc) => loc.code !== locale.value),
)
const switchLocale = (locale: string) => {
return switchLocalePath(locale)
}
</script> This setup seems to works on the server using |
Sorry, no. I'm using |
My issue explained above regarding duplicate crawled dynamic routes has been fixed since version export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
strategy: 'prefix_except_default',
defaultLocale: 'de',
locales: [
{
name: 'Deutsch',
code: 'de',
},
{
name: 'English',
code: 'en',
},
],
},
}) |
What about this?
definePageMeta({
middleware: [
async function ({ meta, params }) {
const data = await useNewsDetails(params.slug); // fetch
meta.nuxtI18n = data.value.localizations || {};
},
],
});
von Outlook für Android<https://aka.ms/AAb9ysg>
…________________________________
From: Fabian ***@***.***>
Sent: Tuesday, August 22, 2023 4:14:06 PM
To: nuxt-modules/i18n ***@***.***>
Cc: Mario Kreidl ***@***.***>; Mention ***@***.***>
Subject: Re: [nuxt-modules/i18n] Feature: Dynamic route parameters should not be set using definePageMeta (Issue #1736)
I gave this another try but still can't get dynamic route parameters to work properly with data that is being fetched async.
As mentioned in the docs<https://v8.i18n.nuxtjs.org/guide/lang-switcher#dynamic-route-parameters> i tried to set them via the compiler macro definePageMeta. The issue seems to be that the compiler macro is being compiled away and can't be referenced with component data, as mentioned in the nuxt docs here<https://nuxt.com/docs/guide/directory-structure/pages/#page-metadata>.
In my case i fetch some data:
<script lang="ts" setup>
const { locale } = useI18n()
const { data } = await useAsyncGql({
operation: 'getSlugPage',
variables: {
site: locale, // de
slug: 'ueber-uns',
},
})
// ...
The data i recive includes a page with a translation:
data: {
page: {
slug: 'ueber-uns',
translation: {
slug: 'about-us'
}
}
}
Then i would like to use this data and set the dynamic routes on nuxtI18n with it:
definePageMeta({
nuxtI18n: {
de: { slug: [data.value.page.slug] }, // should resolve to ueber-uns
en: { slug: [data.value.page.translation.slug] } // should resolve to about-us
},
})
When trying to do so i get the error that data is not defined:
[nuxt] [request error] [unhandled] [500] data is not defined
at ./pages/[...slug].vue:2:21
at ViteNodeRunner.directRequest (./node_modules/vite-node/dist/client.mjs:341:11)
If definePageMeta is not intended to hold fetched component data, how should dynamic routes with fetched data be updated? Any idea how this might be possible?
Many thanks for any help!
My issue explained above regarding duplicate crawled dynamic routes has been fixed since version 8.0.0-rc.3 with this setup using nuxt 3.6.5 and nuxi generate:
export default defineNuxtConfig({
modules: ***@***.***/i18n'],
i18n: {
strategy: 'prefix_except_default',
defaultLocale: 'de',
locales: [
{
name: 'Deutsch',
code: 'de',
},
{
name: 'English',
code: 'en',
},
],
},
})
—
Reply to this email directly, view it on GitHub<#1736 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AICVUTESVOFZ5K7EFJLNV3LXWS5C5ANCNFSM6AAAAAAS7GLPJQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
I have tried this, but get the following error: |
Any news on this issue, or is it beeing tracked somewhere else? |
Any solution that work with nuxt-simple-sitemap? |
Working for me @luca-smartpricing: |
@provoks-land middleware(to) {
to.meta.nuxtI18n.en = {
blog: 'a-simple-blog'
}
} |
It some time but with #2580 merged and released in Try out the new If you run into any bugs please open a new issue with a minimal reproduction 💪 |
Thank you @BobbieGoede - the new release solved my issues and the |
Discussed in #1730
Originally posted by adesombergh December 13, 2022
Hi, thanks for the great work with this module.
I stumbled into a problem with i18n module v8 and nuxt3 and I think it might be interesting to change this in the future, as it makes it difficult to use with dynamic content.
Please, correct me if i'm not doing in right.
The Problem
According to the Dynamic route parameters documentation, to set translations for route params we should use
definePageMeta
like so:However, the Nuxt 3 documentation about
definePageMeta
(right here) says that the page meta object cannot reference the component (or values defined on the component).It means that the content inside
definePageMeta({})
is supposed to be hard-coded and cannot be dynamic, making it impossible to set the page route params dynamically (as they often are). The following code won't work:A solution
Instead of using
definePageMeta
, maybe i18n module could give us a composable likesetParamsTranslations
or whatever, that could be used like this:If anyone agrees I can create an issue (feature)
The text was updated successfully, but these errors were encountered: