completefrontend

Write the codeMedium

Write redirects() in next.config.ts for a shop that moved its pages

Leaf and Kettle is moving its pages. The shop at /shop is now /teas, and each tea's page moves with it: /shop/mint is now /teas/mint. The blog is now the journal: /blog, and every page under it at any depth, moves to the same path under /journal, so /blog/2024/first-flush becomes /journal/2024/first-flush. Some teas were renamed too: renamedTeas in lib/renamed-teas.ts lists each old slug and the new one.

Add redirects() to next.config.ts. The moves are for good, so every redirect is permanent. An old link to a renamed tea reaches its new page in one redirect, not two: /shop/earl-grey-classic and /teas/earl-grey-classic both go straight to /teas/earl-grey. Keep the settings create-next-app wrote.

Write this: next.config.ts
import type { NextConfig } from "next";
import { renamedTeas } from "./lib/renamed-teas";
 
const nextConfig: NextConfig = {
  /* config options here */
  cacheComponents: true,
  partialPrefetching: true,
};
 
export default nextConfig;
lib/renamed-teas.ts (read-only)
/** Teas the shop renamed: each old slug, and the slug its page has now. */
export const renamedTeas = [
  { from: "earl-grey-classic", to: "earl-grey" },
  { from: "moroccan-mint", to: "mint" },
];
The tests your code must pass
  • sends /shop and each tea page under it to /teas, permanently
  • moves the blog and every page under it, at any depth, to /journal
  • sends a renamed tea's old pages to its new one in a single redirect
  • passes the query string on to the new address
  • leaves the new pages alone
  • keeps the settings create-next-app wrote

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…