Skip to content

Potential solution for "missing cross-links between your language slugs" #6

Open
@emreozcan

Description

@emreozcan

Localization SEO is missing currently. It's even noted in the README file:

What is missing:

  • opinion how to localize slugs
    this highly depends where your data comes from. This will probably differ from project to project.
  • cross-links between your language slugs
    like mentioned above, this will differ from project to project. You can find useful resources here:\

This can be solved by implementing Link headers.

This is fairly simple to do, and can be done in the hooks.ts file. Here's what i came up with.

// src/hooks.ts
export const handle: Handle = async ({ event, resolve }) => {
    const response = await resolve(event);

    const [, lang, ...crumbs] = event.url.pathname.split('/')

    const host = event.request.headers.get("host");
    response.headers.set(
        "Link",
        locales.map(
            (locale: Locales) =>
                `<${[host, locale, crumbs].join("/")}>; rel="alternate"; hreflang="${locale}"`
        ).join(", ")
    );

    const body = await response.text();
    return new Response(body.replace('<html lang="en">', `<html lang="${lang}">`), response)
};

It works without a problem in my testing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions