Comments (4)
Start a new pull request in StackBlitz Codeflow.
from nuxt.
You would need to set the cookie returned by useFetch on the HTML response. This isn't done automatically right now, but we might consider doing it for useFetch.
from nuxt.
@danielroe thanks for the answer!
But that doesn't work for me either. I created a composable fetchWithCookie and used it in my code. The sessionId is different every time.
<script setup>
const event = useRequestEvent();
const { data } = await useAsyncData(() => fetchWithCookie(event, '/api/test'));
</script>
<template>
<div>
<p>
If you simply refresh the page, there will be a new session every time.
</p>
<pre>{{ data }}</pre>
<p>
But if you go straight to endpoint:
<a href="/api/test">/api/test</a>, the session id will be remain
permanent. Just what I need!
</p>
</div>
</template>
from nuxt.
I solved my issue via middleware. And it’s strange, it doesn’t work in the endpoint, but works in the middleware. How can it be?
/server/middleware/session.ts
export default defineEventHandler(async (event) => {
console.log('middleware', {
sessionId: event.context.sessionsId,
cookie: getCookie(event, 'seesion-id-test-2'),
})
if (event.context.sessionsId) {
return
}
const session = await getSession(event, {
password: '80d42cfb-1cd2-462c-8f17-e3237d9027e9',
name: 'seesion-id-test-2',
cookie: {
maxAge: 3600,
path: '/',
sameSite: true,
},
})
event.context.sessionsId = session.id
})
from nuxt.
Related Issues (20)
- Route middleware causing duplicate runs on error HOT 2
- Custom useFetch losing original event during SSR HOT 1
- @nuxtbot nuxtbot changed the title Is there a way for Nuxt to render every page in SSR mode, rather than just the first time the page is opened? HOT 1
- Slot "default" invoked outside of the render function when using a composable (contains useLazyAsyncData) inside ClientOnly component HOT 9
- Imported css file with `?raw` should not be resolved HOT 1
- ERROR [nuxt] [request error] [unhandled] [500] [GET] "api/xx/yy": <no response> fetch failed. About using $fetch in server HOT 4
- how to set boost to false in nuxt.config.js HOT 2
- Replace vite-plugin-checker with something else HOT 8
- Unable to import useRuntimeConfig from a layer's server middleware HOT 1
- "UseFetchType" and also transforms the result into the desired output format using another transformer. HOT 1
- Utils Dir in compatibility Mode not auto-imported HOT 5
- You must define the vuex store in the server side and use VuexPersistencePlugin in the plugin list. HOT 3
- Nuxt doesn't update when making CSS changes HOT 2
- Nuxt 4 - allow flat layers directory structure without needing `layers` directory, allow each layer to have its own server routes HOT 1
- Nuxt 4 - Internal server error: Failed to resolve import "@vue/devtools-kit"
- Keep context when opening error page
- Wrapping useFetch in a function causes hydration mismatch HOT 6
- Middleware issue when redirecting with encodeURIComponent HOT 6
- keepalive include Bug
- Server (api routes) not working in new app structure HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nuxt.