ivancic_josip
ivancic_josip
NNuxt
Created by ivancic_josip on 1/28/2025 in #❓・help
Redirects
Is this a valid way to redirect on the server side and will it provide a good redirectCode to search engines? An old route url would only appear on some other website, not inside the website, so it would always run on the server side.
// my-old-route.vue

<script setup lang="ts">
const route = useRoute("my-old-route");

navigateTo(
{
name: "new-route",
},
{
redirectCode: 301,
}
);
</script>
// my-old-route.vue

<script setup lang="ts">
const route = useRoute("my-old-route");

navigateTo(
{
name: "new-route",
},
{
redirectCode: 301,
}
);
</script>
8 replies
NNuxt
Created by ivancic_josip on 1/17/2025 in #❓・help
Nuxt test utils and provide/inject
Any idea how to test a component that is using provide and inject, without creating a test only wrapper. This is what I tried, based on the docs https://test-utils.vuejs.org/api/#global-provide
// in symbols
export const SearchContextKey: InjectionKey<SearchContext> =
Symbol("SearchContext");

// in test
renderSuspended(SearchResultCard, {
props: {
article: { ...article, ...articleData },
},
global: {
provide: {
[SearchContextKey]: {
type: 'article'
}
}
}
});
// in symbols
export const SearchContextKey: InjectionKey<SearchContext> =
Symbol("SearchContext");

// in test
renderSuspended(SearchResultCard, {
props: {
article: { ...article, ...articleData },
},
global: {
provide: {
[SearchContextKey]: {
type: 'article'
}
}
}
});
4 replies
NNuxt
Created by ivancic_josip on 9/17/2024 in #❓・help
Redirecting from a route on server side
Hello, I'm not sure why this is not working, I get a 404 even though the code gets to the navigateTo call, anyone have any idea?
<script setup lang="ts">
const issn = useRoute("issn").params.issn;
const isParamIssn = issn.includes("-");

if (isParamIssn) {
await redirectToJournal();
} else {
await redirectToArticle();
}

async function redirectToJournal() {
const data = await JournalsService.fetchSingleJournalNameByIssn(issn);

if (data.nameSystem) {
navigateTo(
{
...
},
{
redirectCode: 301,
}
);
} else {
throw createError({
statusCode: 404,
});
}
}

async function redirectToArticle() {
const articleId = issn;
const data = await ArticlesService.fetchRedirectDataById(articleId);

if (data.identifier) {
navigateTo(
{
...
},
{
redirectCode: 301,
}
);
} else {
throw createError({
statusCode: 404,
});
}
}
</script>
<script setup lang="ts">
const issn = useRoute("issn").params.issn;
const isParamIssn = issn.includes("-");

if (isParamIssn) {
await redirectToJournal();
} else {
await redirectToArticle();
}

async function redirectToJournal() {
const data = await JournalsService.fetchSingleJournalNameByIssn(issn);

if (data.nameSystem) {
navigateTo(
{
...
},
{
redirectCode: 301,
}
);
} else {
throw createError({
statusCode: 404,
});
}
}

async function redirectToArticle() {
const articleId = issn;
const data = await ArticlesService.fetchRedirectDataById(articleId);

if (data.identifier) {
navigateTo(
{
...
},
{
redirectCode: 301,
}
);
} else {
throw createError({
statusCode: 404,
});
}
}
</script>
3 replies