hono icon indicating copy to clipboard operation
hono copied to clipboard

Invoke callback for `ssgParams()` only once

Open usualoma opened this issue 1 year ago • 7 comments

What is the feature you are proposing?

Same issue as @nakasyou mentions below, but now ssgParams() is pure middleware and also not handling special skipping internally, so it is called many times during SSG. https://github.com/honojs/hono/pull/2169#issuecomment-1934040823

I think the following tests need to be success (now it will fail)

describe('ssgParams', () => {
  it('callback', async() => {
    const app = new Hono()
    const cb = vi.fn(() => [{ post: '1' }, { post: '2' }])
    app.get(
      '/post/:post',
      ssgParams(cb),
      (c) => c.html(<h1>{c.req.param('post')}</h1>)
    )
    const fsMock: FileSystemModule = {
      writeFile: vi.fn(() => Promise.resolve()),
      mkdir: vi.fn(() => Promise.resolve()),
    }
    await toSSG(app, fsMock)

    expect(cb).toHaveBeenCalledTimes(1)
  })
})

Does ssgParams() need to be a REAL middleware?

I understand that it is better to have it in app.get(...) for the appearance of the definition. However, if it functions as REAL middleware, it will be called on every request at the time of creation, which is a useless overhead.

I think it would be better to introduce the concept of "PSEUDO middleware" in hono and solve the problem as shown in the following PR. The test passed without any major changes.

#2174

usualoma avatar Feb 08 '24 23:02 usualoma

How about that? @yusukebe @sor4chi @watany-dev @nakasyou

usualoma avatar Feb 08 '24 23:02 usualoma

@usualoma

Hi!

Interesting approach. I'll take a look at the details later. But can you think of any other PSEUDO middleware?

yusukebe avatar Feb 09 '24 00:02 yusukebe

@yusukebe Hmmm no, there is no particular other use for PSEUDO middleware. But I still don't think "send a request to the entire routing to get the value from ssgParams()" is a good approach, I think we should just invoke ssgParams().

usualoma avatar Feb 09 '24 01:02 usualoma

@usualoma

Thanks. I also can't think of other use cases.

But I still don't think "send a request to the entire routing to get the value from ssgParams()" is a good approach, I think we should just invoke ssgParams().

Totally agree. It's not a good approach.

Actually, I don't want to change hono-base. If there are other use cases for PSEUDO middleware, I'd be happy to do that. But, hmm.

yusukebe avatar Feb 09 '24 01:02 yusukebe

@yusukebe Assuming we don't change hono-base, I think we can do this as well by changing only src/helper/ssg/index.ts. https://github.com/honojs/hono/pull/2178

usualoma avatar Feb 09 '24 03:02 usualoma

@usualoma

Thanks, I'll take a look at it later (I'll be a little bit busy to release v4 and have YAPC!)

yusukebe avatar Feb 09 '24 05:02 yusukebe