Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hono Context / Cookie cached ? and compress plugin problem #7

Closed
fortezhuo opened this issue Aug 23, 2024 · 7 comments
Closed

Hono Context / Cookie cached ? and compress plugin problem #7

fortezhuo opened this issue Aug 23, 2024 · 7 comments

Comments

@fortezhuo
Copy link

fortezhuo commented Aug 23, 2024

Hi,

I played with Hono and Vike Node, and found 2 issues

  • while using cookies
  • compress plugin cause page rendered not correctly

How to reproduce :

  1. Clone below repo / copy below script
  2. Run http://localhost:3000/redirect to refresh cookie
  3. Page will be redirect to / and cookie will be displayed
  4. Run step no. 2 and get cookie value on page will display prev value

Source : https://github.com/fortezhuo/vike-node-hono

app.js

const app = new Hono()
app.post('_telefunc', async (ctx) => {
  const { url, method } = ctx.req
  const body = await ctx.req.text()
  const httpResponse = await telefunc({ url, method, body })
  return new Response(httpResponse.body, {
    status: httpResponse.statusCode
  })
})
app.get("/redirect",async (c)=>{
 setCookie(c,"cookie",Math.random())
  return c.redirect("http://localhost:3000")
})
app.get("/cookie",async (c)=>{
 const cookie = getCookie(c,"cookie")
   return c.json({cookie})
 })
 
app.use(vike({
  pageContext(c){
    const cookie = getCookie(c,"cookie")
     return {
      cookie
    }
  },
  static:{
    root:"./dist/client"
  }
}))

config.js

const config = {
  passToClient: ["cookie"],
  // https://vike.dev/extends
  extends: vikeReact,
  prerender: false
}

+Page.jsx


function Page() {
  const { cookie } = usePageContext()
  return (
    <>
      <h1>Welcome</h1>
      This page is: &&&
      <ul>
        <li>Pre-rendered</li>
        <li>Static html generated</li>
        <li>Interactive</li>
      </ul>
      cookie : {cookie}
      <Counter />
    </>
  )
}

Screenshot :
Vike Page has cookie cached
image

Hono Page get cookie correctly
image

  1. Build the repo with compress plugin enabled
image

Updated :
I have reproduce using batijs and https://github.com/phonzammi/vike-hono-example 's template, seems compress plugin problem related to vike-react vikejs/vike-react#143

@fortezhuo fortezhuo changed the title Hono Context / Cookie cached ? Hono Context / Cookie cached ? and compress plugin problem Aug 23, 2024
@brillout
Copy link
Member

brillout commented Aug 23, 2024

Regarding the compression issue, try [email protected]. Does it solve the issue?

@fortezhuo
Copy link
Author

Regarding the compression issue, try [email protected]. Does it solve the issue?

I have tried commit-d2ca712, the issue still persist

@nitedani
Copy link
Member

nitedani commented Aug 23, 2024

compress plugin cause page rendered not correctly

This is an issue in the hono/compress package: https://github.com/honojs/hono/blob/main/src/middleware/compress/index.ts

It compresses everything regardless of what the response is. Even if the response is already compressed, uncompressible, etc.. it compresses the response again. vike-node already handles compression for the html/static assets, and hono/compress compresses it again without checking anything. If you want to use hono/compress you can set compress: false to avoid double compression, until this issue is fixed in hono/compress .

@nitedani
Copy link
Member

The stale context issue is fixed in [email protected]

@nitedani
Copy link
Member

honojs/hono#3317

@fortezhuo
Copy link
Author

The stale context issue is fixed in [email protected]

Confirmed worked now.. with 0.1.16 . I will close this issue.. thanks for your quick response and solution

@brillout
Copy link
Member

double compression

I wonder if there is an easy way to detect this and then show a assertWarning()/assertUsage(). Could be worth it if it's easy to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants