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

Custom PageContext returns undefined for all custom values #1537

Closed
alycda opened this issue Mar 4, 2024 · 6 comments
Closed

Custom PageContext returns undefined for all custom values #1537

alycda opened this issue Mar 4, 2024 · 6 comments
Labels

Comments

@alycda
Copy link

alycda commented Mar 4, 2024

Description

Hi, I have followed the guides at https://vike.dev/pageContext#custom but the values I pass are ALWAYS undefined, server-side and client-side.

...
import cookieParser from 'cookie-parser'
...

  const app = express()

  app.use(compression())
  app.use(cookieParser())

...

    const pageContextInit = {
      urlOriginal: req.originalUrl,

      // pageContext.headers – e.g. make cookies available over pageContext.headers.cookies
      headers: req.headers,

      cookies: req.cookies,
    }

// +config.ts

export default {
  passToClient: [
    'headers',
    'cookies',
  ]
}

terminal console output for headers and cookies is undefined, as well as the console output in Browser DevTools. I have also tried adding a hardcoded string value and that also returns undefined.

@alycda alycda added the bug 💥 label Mar 4, 2024
@phonzammi
Copy link
Member

Hi @alycda
Can you provide a minimal repro, please?

@alycda
Copy link
Author

alycda commented Mar 4, 2024

Hi, I found the issue. This project is within an NX monorepo, so the command I used to run was pnpm nx serve apps/env-switcher which uses @nx/vite:dev-server. But running with pnpm dev seems to work (I assume thanks to ts-node).

@phonzammi
Copy link
Member

By default, your server code isn't processed by Vite: issue#562

@alycda
Copy link
Author

alycda commented Mar 4, 2024

By default, your server code isn't processed by Vite: issue#562

Yes, I read that comment at the top of the file and I did restart whenever changes were made but the custom page context items I added were always undefined, including a simple string.

@phonzammi
Copy link
Member

I'm sorry. The point is when you run the dev server, what script does it execute, if it's vite or vite dev then it doesn't run your express server.

@alycda
Copy link
Author

alycda commented Mar 4, 2024

ah, I see. that file (index.ts) wasn't automagically executed by @nx/vite:dev-server so the values were undefined (and I correctly got errors regarding headers and cookies not being passed to client without changes to +config.ts)

@alycda alycda closed this as completed Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants