Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: add current-date resource handler to CLI templates (#5439)
zehjotkahc3847074cd66
diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsxindex ba33de0fae..7a3e0d6e37 100644--- a/packages/cli/templates/defaults/app/route-templates/html.tsx+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx@@ -54,6 +54,24 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}+ if (isLocalResource(input, "current-date")) {+ const now = new Date();+ // Normalize to midnight UTC to prevent hydration mismatches+ const startOfDay = new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())+ );+ const data = {+ iso: startOfDay.toISOString(),+ year: startOfDay.getUTCFullYear(),+ month: startOfDay.getUTCMonth() + 1, // 1-12 instead of 0-11+ day: startOfDay.getUTCDate(),+ timestamp: startOfDay.getTime(),+ };+ const response = new Response(JSON.stringify(data));+ response.headers.set("content-type", "application/json; charset=utf-8");+ return Promise.resolve(response);+ }+return cachedFetch(projectId, input, init);};diff --git a/packages/cli/templates/defaults/app/route-templates/xml.tsx b/packages/cli/templates/defaults/app/route-templates/xml.tsxindex 779d3fb0c8..3a8ccf6a72 100644--- a/packages/cli/templates/defaults/app/route-templates/xml.tsx+++ b/packages/cli/templates/defaults/app/route-templates/xml.tsx@@ -22,6 +22,24 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}+ if (isLocalResource(input, "current-date")) {+ const now = new Date();+ // Normalize to midnight UTC to prevent hydration mismatches+ const startOfDay = new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())+ );+ const data = {+ iso: startOfDay.toISOString(),+ year: startOfDay.getUTCFullYear(),+ month: startOfDay.getUTCMonth() + 1, // 1-12 instead of 0-11+ day: startOfDay.getUTCDate(),+ timestamp: startOfDay.getTime(),+ };+ const response = new Response(JSON.stringify(data));+ response.headers.set("content-type", "application/json; charset=utf-8");+ return Promise.resolve(response);+ }+return fetch(input, init);};diff --git a/packages/cli/templates/react-router/app/route-templates/html.tsx b/packages/cli/templates/react-router/app/route-templates/html.tsxindex b4b47d0cfd..77ffc2f4b8 100644--- a/packages/cli/templates/react-router/app/route-templates/html.tsx+++ b/packages/cli/templates/react-router/app/route-templates/html.tsx@@ -53,6 +53,24 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}+ if (isLocalResource(input, "current-date")) {+ const now = new Date();+ // Normalize to midnight UTC to prevent hydration mismatches+ const startOfDay = new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())+ );+ const data = {+ iso: startOfDay.toISOString(),+ year: startOfDay.getUTCFullYear(),+ month: startOfDay.getUTCMonth() + 1, // 1-12 instead of 0-11+ day: startOfDay.getUTCDate(),+ timestamp: startOfDay.getTime(),+ };+ const response = new Response(JSON.stringify(data));+ response.headers.set("content-type", "application/json; charset=utf-8");+ return Promise.resolve(response);+ }+return cachedFetch(projectId, input, init);};diff --git a/packages/cli/templates/react-router/app/route-templates/xml.tsx b/packages/cli/templates/react-router/app/route-templates/xml.tsxindex 7cc6623097..05ad59f6c9 100644--- a/packages/cli/templates/react-router/app/route-templates/xml.tsx+++ b/packages/cli/templates/react-router/app/route-templates/xml.tsx@@ -22,6 +22,24 @@ const customFetch: typeof fetch = (input, init) => {return Promise.resolve(response);}+ if (isLocalResource(input, "current-date")) {+ const now = new Date();+ // Normalize to midnight UTC to prevent hydration mismatches+ const startOfDay = new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())+ );+ const data = {+ iso: startOfDay.toISOString(),+ year: startOfDay.getUTCFullYear(),+ month: startOfDay.getUTCMonth() + 1, // 1-12 instead of 0-11+ day: startOfDay.getUTCDate(),+ timestamp: startOfDay.getTime(),+ };+ const response = new Response(JSON.stringify(data));+ response.headers.set("content-type", "application/json; charset=utf-8");+ return Promise.resolve(response);+ }+return fetch(input, init);};diff --git a/packages/cli/templates/ssg/app/route-templates/html/+data.ts b/packages/cli/templates/ssg/app/route-templates/html/+data.tsindex a4dd7565ea..bcd1ef6eda 100644--- a/packages/cli/templates/ssg/app/route-templates/html/+data.ts+++ b/packages/cli/templates/ssg/app/route-templates/html/+data.ts@@ -1,8 +1,34 @@import type { PageContextServer } from "vike/types";import { redirect } from "vike/abort";-import { loadResources } from "@webstudio-is/sdk/runtime";+import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";import { getPageMeta, getResources } from "__SERVER__";+const customFetch: typeof fetch = (input, init) => {+ if (typeof input !== "string") {+ return fetch(input, init);+ }++ if (isLocalResource(input, "current-date")) {+ const now = new Date();+ // Normalize to midnight UTC to prevent hydration mismatches+ const startOfDay = new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())+ );+ const data = {+ iso: startOfDay.toISOString(),+ year: startOfDay.getUTCFullYear(),+ month: startOfDay.getUTCMonth() + 1, // 1-12 instead of 0-11+ day: startOfDay.getUTCDate(),+ timestamp: startOfDay.getTime(),+ };+ const response = new Response(JSON.stringify(data));+ response.headers.set("content-type", "application/json; charset=utf-8");+ return Promise.resolve(response);+ }++ return fetch(input, init);+};+export const data = async (pageContext: PageContextServer) => {const url = new URL(pageContext.urlOriginal, "http://url");const headers = new Headers(pageContext.headers ?? {});@@ -18,7 +44,10 @@ export const data = async (pageContext: PageContextServer) => {pathname: url.pathname,};- const resources = await loadResources(fetch, getResources({ system }).data);+ const resources = await loadResources(+ customFetch,+ getResources({ system }).data+ );const pageMeta = getPageMeta({ system, resources });if (pageMeta.redirect) {