I’ve a nuxt application, that is deployed using S3 and CF on AWS. The canonical URL is getting changed on production, but working correctly on localhost. Here is my folder structure:
ISSUE:
Inside these pages I’ve added head()
function and added canonical tags custom to each page. Say, For ./blogs
home page, below is the code:
head() {
return {
title: 'StackWealth Blog: Financial Insights & Investment Tips',
meta: [
{
hid: 'description',
name: 'description',
content: '...'
}
],
link: [
{
hid: 'canonical',
rel: 'canonical',
href: `https://stackwealth.in/blogs`
}
]
};
},
If I check this on localhost, the link tag is coming correctly. But as soon as I build and deploy to production, this URL automatically get replaced by https://stackwealth.in/
i.e. removing route names from the host URL.
Here is my nuxt.config.js:
import { getBlogRoutes } from './utils';
require('dotenv').config();
export default {
ssr: true,
server: {
port: process.env.PORT || 5100,
host: '0.0.0.0' // default: localhost
},
head: {
title: '...',
meta: [
{
charset: 'utf-8'
},
{
hid: 'description',
name: 'description',
content: '...'
},
...
],
link: [
{
rel: 'icon',
type: 'image/x-icon',
href: '/favicon.png'
}
]
},
css: [
'@/assets/scss/app.scss'
],
plugins: [...],
buildModules: ['nuxt-compress'],
modules: [
...
],
generate: {
routes: () => getBlogRoutes() /// there is Util function that pull all dynamic URLs
},
...
};
Expectations:
The URL inside href
of <link rel="canonical" href="...">
should be the link of page instead of the host URL.
2
Answers
So basically, the page was loading with a 403 error
Since I've subdirectories such as http://www.example.com/about/. The index.html needs to be added after the subdirectory i.e "https://www.example.com/about/index.html" so the page will load without 403 errors.
So for this I've done a few steps.
/
. i.e., https://example.co/about should be https://example.co/about/. This can be done by addingtrailingSlash: true
insiderouter
key ofnuxt.config.js
. Don't forget to change all<NuxtLink to="">
code inside your HTML or if you're using this.$route.path('')redirect.js
file inside./middleware/
of the project and include this file again innuxt.config.js
insideserverMiddleware
key. This file will contain the code to redirect all the URL that are not ending with/
to the URL ending with/
. Check code below.PS - Make sure while editing the AWS Lambda you are saving the details. Also, somehow L@E Node.js 18.x was throwing ES6 module error, so use
export const handler = async () => {}
instead ofexports.handler = async () => {}
. Weird but yeah!redirect.js
You probably used a component inside the page and inside that component you change the canonical value in the head tag.