diff --git a/examples/basic/package.json b/examples/basic/package.json index dd9744c3..3f2f9a2c 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -15,7 +15,8 @@ "typescript": "^5.3.3", "vike": "^0.4.160", "vike-react": "^0.4.4", - "vite": "^5.0.12" + "vite": "^5.0.12", + "devalue": "^4.3.2" }, "type": "module" } diff --git a/examples/basic/pages/index/+Page.tsx b/examples/basic/pages/index/+Page.tsx index 75b3dbbe..8bfa28af 100644 --- a/examples/basic/pages/index/+Page.tsx +++ b/examples/basic/pages/index/+Page.tsx @@ -1,19 +1,62 @@ export default Page -import React from 'react' -import { Counter } from './Counter' +import React, { useId } from 'react' +import { uneval } from 'devalue' +const date = new Date('2024-01-01 00:00+07:00') function Page() { return ( <> -

My Vike + React app

- This page is: - + {/*

{date.toLocaleString()}

*/} + date.toLocaleString()} /> + + ) +} + +const DateTime = ({ date, format }: { date: Date; format: (date: Date) => string }) => { + const key = useId() + const id = `__placeholder__${key}` + const idScript = `__placeholder__${key}__script` + const allFormattedDates: { [key: string]: string } = {} + const localOffsetHours = -(new Date().getTimezoneOffset() / 60).toFixed(1) + let localFormattedDate: string + if (import.meta.env.SSR) { + const allOfsetsHours = [ + +0.0, +1.0, +2.0, +3.0, +3.5, +4.0, +4.5, +5.5, +5.75, +6.0, +6.5, +7.0, +8.0, +8.75, +9.0, +9.5, +10.0, +10.5, + +11.0, +12.0, +12.75, +13.0, +13.75, +14.0, -1.0, -2.0, -2.5, -3.0, -3.5, -4.0, -4.5, -5.0, -6.0, -7.0, -8.0, + -9.0, -9.5, -10.0, -11.0, -12.0 + ] + for (const offsetHours of allOfsetsHours) { + allFormattedDates[offsetHours] = format( + new Date(date.getTime() + (offsetHours - localOffsetHours) * 60 * 60 * 1000) + ).replaceAll('\u202F', ' ') + } + localFormattedDate = allFormattedDates[localOffsetHours] + } else { + //@ts-ignore + localFormattedDate = window[`localFormattedDate${key}`] + } + + return ( + <> +
{localFormattedDate}
+ ) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fe4e49c9..d64c0f3f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,6 +27,9 @@ importers: '@vitejs/plugin-react': specifier: ^4.2.1 version: 4.2.1(vite@5.0.12) + devalue: + specifier: ^4.3.2 + version: 4.3.2 node-fetch: specifier: ^3.3.2 version: 3.3.2