Nuxt 3 Blog: Article Changes When Language Changes, but Slug Does Not Update

I’m working on a multilingual blog using Nuxt 3, and I’m experiencing an issue where the article content updates correctly when the language is changed, but the slug in the route does not update accordingly. Here’s an overview of my setup:

Route Parameter and API Fetching: I’m fetching blog content using the slug from the route and the current language from the locale:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { data: response, refresh } = await useFetch(
`${baseApiUrl}/blog/${route.params.slug}/${locale.value}`,
{
key: `blog-${route.params.slug}-${locale.value}`,
cache: 'no-store',
transform: (response) => {
if (!response || !response.data) {
throw new Error('Invalid response format');
}
return response;
},
}
);
</code>
<code>const { data: response, refresh } = await useFetch( `${baseApiUrl}/blog/${route.params.slug}/${locale.value}`, { key: `blog-${route.params.slug}-${locale.value}`, cache: 'no-store', transform: (response) => { if (!response || !response.data) { throw new Error('Invalid response format'); } return response; }, } ); </code>
const { data: response, refresh } = await useFetch(
  `${baseApiUrl}/blog/${route.params.slug}/${locale.value}`,
  {
    key: `blog-${route.params.slug}-${locale.value}`,
    cache: 'no-store',
    transform: (response) => {
      if (!response || !response.data) {
        throw new Error('Invalid response format');
      }
      return response;
    },
  }
);

Content Update: When the API response is received, the content updates via processBlogData and processContent functions.
3 .Language Change Behavior: I use Vue I18n for localization, and the article content is correctly fetched in the new language when locale changes. However, the slug in the URL remains the same.

Expected Behavior: When the language is changed, the slug in the URL should update to match the slug for the selected language (e.g., /en/my-article to /es/mi-articulo).
Problem Despite the correct article being loaded, the slug in the URL does not change. For example:

Initial URL: https://example.com/en/my-article After language switch to Spanish: https://example.com/es/my-article (Expected: https://example.com/es/mi-articulo)

1 . Using router.push: I attempted to programmatically update the URL when the language changes: router.push({ name: 'blog-slug', params: { slug: updatedSlug } });

Watching the locale or route: I tried to watch the locale and update the route accordingly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>watch(() => locale.value, (newLocale) => { const newSlug = getSlugForLocale(newLocale); router.push({ name: 'blog-slug', params: { slug: newSlug } }); });
</code>
<code>watch(() => locale.value, (newLocale) => { const newSlug = getSlugForLocale(newLocale); router.push({ name: 'blog-slug', params: { slug: newSlug } }); }); </code>
watch(() => locale.value, (newLocale) => { const newSlug = getSlugForLocale(newLocale); router.push({ name: 'blog-slug', params: { slug: newSlug } }); });

However, this approach either triggers a full page reload or doesn’t correctly synchronize with the language change.

Here’s a simplified version of my blog component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup>
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
const router = useRouter();
const route = useRoute();
const { locale } = useI18n();
const blogSlug = ref('');
const blogTitle = ref('');
const fetchBlogContent = async () => {
const response = await useFetch(
`${baseApiUrl}/blog/${route.params.slug}/${locale.value}`
);
blogSlug.value = response.data.slug;
blogTitle.value = response.data.title;
};
// Watch for locale changes
watch(() => locale.value, () => {
fetchBlogContent();
});
</script>
</code>
<code><script setup> import { ref } from 'vue'; import { useRoute, useRouter } from 'vue-router'; import { useI18n } from 'vue-i18n'; const router = useRouter(); const route = useRoute(); const { locale } = useI18n(); const blogSlug = ref(''); const blogTitle = ref(''); const fetchBlogContent = async () => { const response = await useFetch( `${baseApiUrl}/blog/${route.params.slug}/${locale.value}` ); blogSlug.value = response.data.slug; blogTitle.value = response.data.title; }; // Watch for locale changes watch(() => locale.value, () => { fetchBlogContent(); }); </script> </code>
<script setup>
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';

const router = useRouter();
const route = useRoute();
const { locale } = useI18n();

const blogSlug = ref('');
const blogTitle = ref('');

const fetchBlogContent = async () => {
  const response = await useFetch(
    `${baseApiUrl}/blog/${route.params.slug}/${locale.value}`
  );
  blogSlug.value = response.data.slug;
  blogTitle.value = response.data.title;
};

// Watch for locale changes
watch(() => locale.value, () => {
  fetchBlogContent();
});
</script>

Question How can I make the slug in the route update dynamically when the language changes, while keeping the page content and URL in sync without triggering a full reload? Is there a better approach to handle this in Nuxt 3?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật