|
1 | 1 | import { parse as cookieParse, serialize as cookieSerialize } from 'cookie'
|
2 | 2 | import JsCookie from 'js-cookie'
|
3 | 3 |
|
4 |
| -/** @typedef {import('../../types/internal').ResolvedOptions} ResolvedOptions */ |
| 4 | +/** |
| 5 | + * @typedef {import('../../types/internal').ResolvedOptions} ResolvedOptions |
| 6 | + * @typedef {Required<import('../../types/').DetectBrowserLanguageOptions>} DetectBrowserLanguageOptions |
| 7 | + */ |
5 | 8 |
|
6 | 9 | /**
|
7 | 10 | * Formats a log message, prefixing module's name to it.
|
@@ -185,34 +188,36 @@ export function getLocaleCookie (req, { useCookie, cookieKey, localeCodes }) {
|
185 | 188 | /**
|
186 | 189 | * @param {string} locale
|
187 | 190 | * @param {import('http').ServerResponse | undefined} res
|
188 |
| - * @param {{ useCookie: boolean, cookieDomain: string | null, cookieKey: string, cookieSecure: boolean, cookieCrossOrigin: boolean}} options |
| 191 | + * @param {Pick<DetectBrowserLanguageOptions, 'useCookie' | 'cookieAge' | 'cookieDomain' | 'cookieKey' | 'cookieSecure' | 'cookieCrossOrigin'>} options |
189 | 192 | */
|
190 |
| -export function setLocaleCookie (locale, res, { useCookie, cookieDomain, cookieKey, cookieSecure, cookieCrossOrigin }) { |
| 193 | +export function setLocaleCookie (locale, res, { useCookie, cookieAge, cookieDomain, cookieKey, cookieSecure, cookieCrossOrigin }) { |
191 | 194 | if (!useCookie) {
|
192 | 195 | return
|
193 | 196 | }
|
194 |
| - const date = new Date() |
195 |
| - /** @type {import('cookie').CookieSerializeOptions} */ |
196 |
| - const cookieOptions = { |
197 |
| - expires: new Date(date.setDate(date.getDate() + 365)), |
198 |
| - path: '/', |
199 |
| - sameSite: cookieCrossOrigin ? 'none' : 'lax', |
200 |
| - secure: cookieCrossOrigin || cookieSecure |
201 |
| - } |
202 |
| - |
203 |
| - if (cookieDomain) { |
204 |
| - cookieOptions.domain = cookieDomain |
205 |
| - } |
206 |
| - |
207 | 197 | if (process.client) {
|
208 |
| - // @ts-ignore |
| 198 | + /** @type {import('js-cookie').CookieAttributes} */ |
| 199 | + const cookieOptions = { |
| 200 | + expires: cookieAge, |
| 201 | + path: '/', |
| 202 | + sameSite: cookieCrossOrigin ? 'none' : 'lax', |
| 203 | + secure: cookieCrossOrigin || cookieSecure, |
| 204 | + ...cookieDomain ? { domain: cookieDomain } : {} |
| 205 | + } |
209 | 206 | JsCookie.set(cookieKey, locale, cookieOptions)
|
210 | 207 | } else if (res) {
|
211 | 208 | let headers = res.getHeader('Set-Cookie') || []
|
212 | 209 | if (!Array.isArray(headers)) {
|
213 | 210 | headers = [String(headers)]
|
214 | 211 | }
|
215 | 212 |
|
| 213 | + /** @type {import('cookie').CookieSerializeOptions} */ |
| 214 | + const cookieOptions = { |
| 215 | + maxAge: cookieAge * 60 * 60 * 24, // in seconds |
| 216 | + path: '/', |
| 217 | + sameSite: cookieCrossOrigin ? 'none' : 'lax', |
| 218 | + secure: cookieCrossOrigin || cookieSecure, |
| 219 | + ...cookieDomain ? { domain: cookieDomain } : {} |
| 220 | + } |
216 | 221 | const redirectCookie = cookieSerialize(cookieKey, locale, cookieOptions)
|
217 | 222 | headers = headers.filter(header => {
|
218 | 223 | const cookie = cookieParse(header)
|
|
0 commit comments