Skip to content

SSG: Supports redirects #4389

@3w36zj6

Description

@3w36zj6

What is the feature you are proposing?

Redirects are essential for SSG to ensure users and search engines are properly directed when URLs change. This supports smooth migrations, maintains SEO value, and prevents broken links.

Feature Idea

When an endpoint that performs a redirect is defined as follows:

import { Hono } from 'hono'
import { toSSG } from 'hono/ssg'
import fs from 'fs/promises'

const app = new Hono()

app.get("/old", (c) => {
  return c.redirect("/new")
})

app.get("/new", (c) => {
  return c.render(
    <>
      <title>New Page</title>
      <meta name='description' content='This is the new page.' />
      new page content.
    </>
  )
})

toSSG(app, fs)

export default app

old.html is generated as follows:

<!DOCTYPE html>
<title>Redirecting to: /new</title>
<meta http-equiv="refresh" content="0;url=/new" />
<meta name="robots" content="noindex" />
<link rel="canonical" href="/new" />
<body>
  <a href="/new">Redirecting from <code>/old</code> to <code>/new</code></a>
</body>

Alternatively, an option can be added to ToSSGOptions in toSSG to define a mapping for redirects. This is inspired by Astro1.

export interface ToSSGOptions {
    dir?: string;
    beforeRequestHook?: BeforeRequestHook | BeforeRequestHook[];
    afterResponseHook?: AfterResponseHook | AfterResponseHook[];
    afterGenerateHook?: AfterGenerateHook | AfterGenerateHook[];
    concurrency?: number;
    extensionMap?: Record<string, string>;
    plugins?: SSGPlugin[];
    redirects?: Record<string, string>; // 👈
}

Footnotes

  1. https://docs.astro.build/en/reference/configuration-reference/#redirects

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions