Next.js Discord

Discord Forum

Rewrite to Anchor

Unanswered
Painted Redstart posted this in #help-forum
Open in Discord
Painted RedstartOP
I'd like to rewrite (or equivalently map) a URL in the format path/<item>.anchor to path/<item>#anchor so that navigating to the former sends you to path/item's page scrolled down to the #anchor reference.

My first, naive way of doing this is to use next.config's rewrite function:

    async rewrites() {
        return [
            {
                source: '/path/:slug',
                destination: '/path/:slug#<figure_out_how_to_extract_anchor_from_slug>',
            }
        ]
    }

but, in addition to not knowing how or if i can extract the anchor from the slug, it doesnt seem i can insert any anchor, even for testing purposes

I'd also like to try to stay away from useRouter and useRouter().replace because i don't want to render all the components on path/item client-side, but if the solution requires it then i'll use it

1 Reply

Madeiran sardinella
Middleware?