Skip to content

Commit 9d989b7

Browse files
committed
Make localization work just how google wants it
1 parent 0c92add commit 9d989b7

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

frontend/user/src/routes/sitemap.xml/+server.ts

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ interface Projects {
1111
}[]
1212
}
1313

14+
15+
const langs = ['en', 'es'];
16+
const baseURL = 'https://vectorinterior.design/';
17+
const langURL = `${baseURL}${langs[0]}`;
18+
19+
// Generates the `xhtml:link` tags for all languages for the given url relative to the base.
20+
// Example: For url `https://vectorinterior.design/en/proyectos` pass only `/proyectos`
21+
function generateLocalizedTags(relUrl: string): string {
22+
let tags = langs.map(lang => {
23+
return `<xhtml:link rel="alternate" hreflang="${lang}" href="${baseURL}${lang}${relUrl}"/>`;
24+
}).join('\n');
25+
26+
return tags;
27+
}
28+
1429
export async function GET({ fetch }) {
1530
const query = `
1631
query projects {
@@ -28,63 +43,63 @@ export async function GET({ fetch }) {
2843
`;
2944

3045
const projects: Projects[] = (await graphql(query, {}, fetch)).projects;
31-
const baseURL = 'https://vectorinterior.design/';
32-
const langs = ['en', 'es']
46+
3347
return new Response(
3448
`
35-
<?xml version="1.0" encoding="UTF-8" ?>
49+
<?xml version="1.0" encoding="UTF-8" ?>
3650
<urlset
3751
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
38-
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
39-
xmlns:xhtml="https://www.w3.org/TR/xhtml1"
40-
>
41-
${langs.map(lang => {
42-
const langUrl = `${baseURL}${lang}`;
43-
return `
44-
<url>
45-
<loc>${langUrl}</loc>
46-
<changefreq>yearly</changefreq>
47-
<priority>1.0</priority>
48-
</url>
49-
<url>
50-
<loc>${langUrl}/esculturas</loc>
51-
<changefreq>yearly</changefreq>
52-
<priority>0.7</priority>
53-
</url>
54-
<url>
55-
<loc>${langUrl}/proyectos</loc>
56-
<changefreq>yearly</changefreq>
57-
<priority>0.8</priority>
58-
</url>
59-
${projects.map(project => {
60-
const projectUrl = `${langUrl}/proyectos/${project.id}`;
61-
return `
52+
xmlns:xhtml="http://www.w3.org/1999/xhtml"
53+
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
54+
<url>
55+
<loc>${langURL}</loc>
56+
${langs.map(lang => {
57+
// Not using `generateLocalizedUrl` here since its the base path
58+
return `<xhtml:link rel="alternate" hreflang="${lang}" href="${baseURL}${lang}"/>`
59+
}).join('\n')}
60+
<changefreq>yearly</changefreq>
61+
<priority>1.0</priority>
62+
</url>
63+
<url>
64+
<loc>${langURL}/esculturas</loc>
65+
${generateLocalizedTags("/esculturas")}
66+
<changefreq>yearly</changefreq>
67+
<priority>0.7</priority>
68+
</url>
69+
<url>
70+
<loc>${langURL}/proyectos</loc>
71+
${generateLocalizedTags("/proyectos")}
72+
<changefreq>yearly</changefreq>
73+
<priority>0.8</priority>
74+
</url>
75+
76+
${projects.map(project => {
77+
const projectUrl = `${langURL}/proyectos/${project.id}`;
78+
return `
6279
<url>
6380
<loc>${projectUrl}</loc>
81+
${generateLocalizedTags(`/proyectos/${project.id}`)}
6482
<changefreq>yearly</changefreq>
6583
<priority>0.5</priority>
66-
<xhtml:link rel="alternate" hreflang="${lang}" href="${projectUrl}" />
84+
6785
${project.spaces.map(space => {
68-
return space.images.map(image => {
69-
return ['descriptionEn', 'descriptionEs'].map((_, i) => {
70-
return `
86+
return space.images.map(image => {
87+
return ['descriptionEn', 'descriptionEs'].map((lang, i) => {
88+
return `
7189
<image:image>
7290
<image:loc>https:${image.imageUrl}</image:loc>
7391
${(i === 0 && image.descriptionEn) || (i === 1 && image.descriptionEs) ?
74-
`<image:caption>${i === 0 ? image.descriptionEn : image.descriptionEs}</image:caption>`
75-
: ''}
92+
`<image:caption xml:lang="${lang}">${i === 0 ? image.descriptionEn : image.descriptionEs}</image:caption>`
93+
: ''}
7694
</image:image>
7795
`.trim();
78-
}).join('');
79-
}).join('');
80-
}).join('')}
96+
}).join('\n');
97+
}).join('\n');
98+
}).join('\n')}
8199
</url>
82100
`.trim();
83-
})
84-
}
85-
`
86-
}).join('')}
87-
101+
})
102+
}
88103
</urlset>
89104
`.trim(),
90105
{

0 commit comments

Comments
 (0)