honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
19.03k stars 541 forks source link

SSG route called #3088

Open RobertSasak opened 3 months ago

RobertSasak commented 3 months ago

What version of Hono are you using?

4.4.11

What runtime/platform is your app running on?

NodeJS

What steps can reproduce the bug?

// build.ts
import fs from 'fs/promises'
import { Hono } from 'hono'
import { ssgParams, toSSG } from 'hono/ssg'

const app = new Hono()
app.get(
    '/shops/:id',
    ssgParams(() => [{ id: 'shop1' }]),
    async (c) => {
        const id = c.req.param('id')
        console.warn(id)
        return c.html(id)
    },
)

toSSG(app, fs)
$ npx vite-node src/build.ts

What is the expected behavior?

It is expected to log

shop1

What do you see instead?

:id
shop1

Additional information

Route is called twice instead of once. First time the route is called the parameter id is not replaced with the actual value but stays ":id".

I think it might be related to https://github.com/honojs/hono/issues/2175

EdamAme-x commented 3 months ago

I'll look abt this

EdamAme-x commented 3 months ago

hi @RobertSasak Does this mean that when I access "/shop1" it is logged? Or are you talking about "/:id is also generated"?

EdamAme-x commented 3 months ago

https://github.com/honojs/hono/blob/main/src/helper/ssg/ssg.ts#L228

EdamAme-x commented 3 months ago

https://github.com/honojs/hono/blob/main/src/helper/ssg/middleware.ts#L43

RobertSasak commented 3 months ago

I run vite to command to produce static pages. ssgParams define only one page to be generated. It has id="shop1". However actual route get called twice. First time with a id=":id" and second time with id="shop1".

Only one static page is actually produced on the disk(shop1.html). However during my test I also saw ":id.html" created on the disk. I am not sure how to reproduce it.

EdamAme-x commented 3 months ago

hmm... Looking at the code, it does not seem to be generated @RobertSasak

yusukebe commented 3 months ago

Hi @RobertSasak

This is a known issue. We may fix it.

yusukebe commented 3 months ago

@watany-dev @sor4chi @nakasyou @usualoma Do you have any idea?

RobertSasak commented 3 months ago

Thank you for following up this issue. I don't think it is necessary to spend energy on it as there is a simple workaround for it. I expected it will be fixed onced the question whether SSG should be middleware or not.

const id = c.req.param('id')
if (id==':id') {
    return c.html('Ignored', 500) // https://github.com/honojs/hono/issues/3088
}
nakasyou commented 3 months ago

When I implemented this, I couldn't find how to resolve this.

However, application users can access /shops/:id when it isn't on SSG, so I think there is no problem.

nakasyou commented 3 months ago

I have an idea.

What do you think about allowing ssgParams to response params as JSON when request is for SSG? In this solution, it will be returned early, so after handler will not be called. But we must set disableSSG and onlySSG before ssgParams, it may be breaking change.