cloudcommerce 0.23.2 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -0
- package/ecomplus-stores/barra-doce/functions/many/package.json +3 -3
- package/ecomplus-stores/barra-doce/functions/ssr/content/layout.json +0 -6
- package/ecomplus-stores/barra-doce/functions/ssr/content/settings.json +17 -0
- package/ecomplus-stores/barra-doce/functions/ssr/package.json +6 -6
- package/ecomplus-stores/barra-doce/functions/ssr/public/robots.txt +2 -4
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/AccountMenu.vue +1 -10
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/Banner.vue +4 -6
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/PitchBar.vue +2 -3
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/ProductCard.vue +4 -5
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/ProductShelf.vue +1 -4
- package/ecomplus-stores/barra-doce/functions/ssr/src/components/ShopHeader.vue +2 -7
- package/ecomplus-stores/barra-doce/functions/ssr/tailwind.config.cjs +1 -1
- package/ecomplus-stores/barra-doce/functions/with-apps/package.json +3 -3
- package/ecomplus-stores/barra-doce/package.json +2 -2
- package/ecomplus-stores/iluminim/functions/many/package.json +3 -3
- package/ecomplus-stores/iluminim/functions/ssr/content/layout.json +0 -6
- package/ecomplus-stores/iluminim/functions/ssr/content/settings.json +16 -0
- package/ecomplus-stores/iluminim/functions/ssr/package.json +6 -6
- package/ecomplus-stores/iluminim/functions/ssr/public/robots.txt +2 -4
- package/ecomplus-stores/iluminim/functions/ssr/src/components/AccountMenu.vue +1 -10
- package/ecomplus-stores/iluminim/functions/ssr/src/components/Banner.vue +4 -6
- package/ecomplus-stores/iluminim/functions/ssr/src/components/PitchBar.vue +2 -3
- package/ecomplus-stores/iluminim/functions/ssr/src/components/ProductCard.vue +4 -5
- package/ecomplus-stores/iluminim/functions/ssr/src/components/ProductShelf.vue +1 -4
- package/ecomplus-stores/iluminim/functions/ssr/src/components/ShopHeader.vue +2 -7
- package/ecomplus-stores/iluminim/functions/ssr/tailwind.config.cjs +1 -1
- package/ecomplus-stores/iluminim/functions/with-apps/package.json +3 -3
- package/ecomplus-stores/iluminim/package.json +2 -2
- package/package.json +8 -8
- package/packages/api/package.json +1 -1
- package/packages/api/tests/index.test.ts +0 -2
- package/packages/apps/affilate-program/package.json +1 -1
- package/packages/apps/correios/package.json +1 -1
- package/packages/apps/custom-payment/lib/custom-payment-create-transaction.js +1 -1
- package/packages/apps/custom-payment/lib/custom-payment-list-payments.js +1 -1
- package/packages/apps/custom-payment/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/datafrete/package.json +1 -1
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/emails/package.json +1 -1
- package/packages/apps/fb-conversions/package.json +2 -2
- package/packages/apps/flash-courier/package.json +1 -1
- package/packages/apps/frenet/package.json +1 -1
- package/packages/apps/galaxpay/lib/galaxpay-create-transaction.js +1 -1
- package/packages/apps/galaxpay/lib/galaxpay-list-payments.js +1 -1
- package/packages/apps/galaxpay/package.json +1 -1
- package/packages/apps/google-analytics/package.json +1 -1
- package/packages/apps/jadlog/package.json +1 -1
- package/packages/apps/loyalty-points/lib/loyalty-create-transaction.js +1 -1
- package/packages/apps/loyalty-points/lib/loyalty-list-payments.js +1 -1
- package/packages/apps/loyalty-points/package.json +1 -1
- package/packages/apps/melhor-envio/package.json +1 -1
- package/packages/apps/mercadopago/lib/mp-create-transaction.js +1 -1
- package/packages/apps/mercadopago/lib/mp-list-payments.js +1 -1
- package/packages/apps/mercadopago/package.json +1 -1
- package/packages/apps/pagarme/lib/pagarme-create-transaction.js +1 -1
- package/packages/apps/pagarme/lib/pagarme-list-payments.js +1 -1
- package/packages/apps/pagarme/package.json +1 -1
- package/packages/apps/paghiper/lib/paghiper-create-transaction.js +1 -1
- package/packages/apps/paghiper/lib/paghiper-list-payments.js +1 -1
- package/packages/apps/paghiper/package.json +1 -1
- package/packages/apps/pix/lib/pix-create-transaction.js +1 -1
- package/packages/apps/pix/lib/pix-list-payments.js +1 -1
- package/packages/apps/pix/package.json +1 -1
- package/packages/apps/tiny-erp/package.json +1 -1
- package/packages/apps/webhooks/package.json +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/emails/package.json +1 -1
- package/packages/emails/tests/sendgrid.test.ts +0 -1
- package/packages/emails/tests/smtp.test.ts +0 -1
- package/packages/eslint/base.eslintrc.cjs +23 -0
- package/packages/eslint/package.json +5 -5
- package/packages/events/package.json +1 -1
- package/packages/feeds/package.json +1 -1
- package/packages/firebase/package.json +1 -1
- package/packages/i18n/package.json +1 -1
- package/packages/modules/package.json +1 -1
- package/packages/passport/package.json +1 -1
- package/packages/ssr/cloudflare/swr-worker.js +94 -26
- package/packages/ssr/lib/firebase/serve-storefront.js +22 -28
- package/packages/ssr/lib/firebase/serve-storefront.js.map +1 -1
- package/packages/ssr/package.json +4 -4
- package/packages/ssr/src/cloudflare/swr-worker.ts +99 -26
- package/packages/ssr/src/firebase/serve-storefront.ts +24 -28
- package/packages/storefront/config/storefront.tailwind.cjs +41 -27
- package/packages/storefront/config/storefront.unocss.cjs +9 -23
- package/packages/storefront/dist/client/_astro/{_...slug_.07c1fa58.css → _...slug_.a786e016.css} +1 -1
- package/packages/storefront/dist/client/_astro/ecom-icon_CDmhA.png +0 -0
- package/packages/storefront/dist/client/_astro/ecom-icon_Z27EX9f.png +0 -0
- package/packages/storefront/dist/client/_astro/ecom-icon_Zo1CLq.png +0 -0
- package/packages/storefront/dist/client/_astro/headphone_22qtrt.avif +0 -0
- package/packages/storefront/dist/client/_astro/headphone_247ud8.avif +0 -0
- package/packages/storefront/dist/client/_astro/headphone_Z1hoMcQ.webp +0 -0
- package/packages/storefront/dist/client/_astro/headphone_Z1j5MXv.webp +0 -0
- package/packages/storefront/dist/client/_astro/logo_144owC.webp +0 -0
- package/packages/storefront/dist/client/_astro/logo_1ug3XF.png +0 -0
- package/packages/storefront/dist/client/_astro/logo_Z1EIN1I.avif +0 -0
- package/packages/storefront/dist/client/_astro/passion_29BEzB.webp +0 -0
- package/packages/storefront/dist/client/_astro/passion_2sykkb.avif +0 -0
- package/packages/storefront/dist/client/_astro/passion_HYj6i.webp +0 -0
- package/packages/storefront/dist/client/_astro/passion_Z18ANwS.avif +0 -0
- package/packages/storefront/dist/client/_astro/passion_ZK2FPz.webp +0 -0
- package/packages/storefront/dist/client/_astro/passion_pVNbE.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_1EUYOa.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_1Sqf3x.png +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_1VeSii.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_2wFECM.png +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_7mg8W.png +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_Kv492.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_Z1DNkkN.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_Z1IS4wy.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect8589_Zt4vbx.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect859_1Bqr36.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect859_1pj5UP.png +0 -0
- package/packages/storefront/dist/client/_astro/rect859_2dVuzq.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect859_HaXay.png +0 -0
- package/packages/storefront/dist/client/_astro/rect859_Z100iy0.png +0 -0
- package/packages/storefront/dist/client/_astro/rect859_Z2i1RBz.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect859_ZMRWqJ.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect859_mPQHw.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect859_xyq7d.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect89_1DkX0O.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect89_2uIqYp.avif +0 -0
- package/packages/storefront/dist/client/_astro/rect89_Z1rGPGT.webp +0 -0
- package/packages/storefront/dist/client/_astro/rect89_ZAjmIj.avif +0 -0
- package/packages/storefront/dist/client/robots.txt +2 -4
- package/packages/storefront/dist/client/sw.js +1 -1
- package/packages/storefront/dist/client/~fallback.html +98 -0
- package/packages/storefront/dist/client/~index.html +134 -0
- package/packages/storefront/dist/server/chunks/{_...c5a36021.mjs → _...5323ec5a.mjs} +4 -11
- package/packages/storefront/dist/server/chunks/{account@_@astro.3e90f29a.mjs → account@_@astro.a02ffb0b.mjs} +4 -11
- package/packages/storefront/dist/server/chunks/{astro.6adaff02.mjs → astro.a7611b51.mjs} +11 -18
- package/packages/storefront/dist/server/chunks/{index@_@astro.66b84ac2.mjs → index@_@astro.42c693b9.mjs} +4 -11
- package/packages/storefront/dist/server/chunks/{index@_@astro.0221a7bd.mjs → index@_@astro.d898498a.mjs} +4 -11
- package/packages/storefront/dist/server/chunks/pages/{_...slug_.astro.501582b9.mjs → _...slug_.astro.0f33557e.mjs} +103 -309
- package/packages/storefront/dist/server/chunks/pages/{account.astro.53400a42.mjs → account.astro.9325adb2.mjs} +1 -1
- package/packages/storefront/dist/server/chunks/pages/{index.astro.f1104ab2.mjs → index.astro.0f31fa39.mjs} +8 -27
- package/packages/storefront/dist/server/chunks/pages/{fallback.astro.8d1c6076.mjs → ~fallback.astro.bd93f01e.mjs} +15 -32
- package/packages/storefront/dist/server/chunks/{fallback@_@astro.9f73db0f.mjs → ~fallback@_@astro.d17419ae.mjs} +4 -11
- package/packages/storefront/dist/server/entry.mjs +47 -16
- package/packages/storefront/dist/server/images.dist.csv +38 -0
- package/packages/storefront/dist/server/images.src.csv +11 -0
- package/packages/storefront/dist/server/renderers.mjs +3 -10
- package/packages/storefront/dist/server/stylesheets.csv +1 -0
- package/packages/storefront/package.json +7 -6
- package/packages/storefront/scripts/build-prod.sh +2 -0
- package/packages/storefront/src/env.d.ts +0 -2
- package/packages/storefront/src/helpers/server-data.ts +9 -2
- package/packages/storefront/src/lib/components/BannerPictures.astro +2 -2
- package/packages/storefront/src/lib/components/Drawer.vue +1 -1
- package/packages/storefront/src/lib/components/PaymentMethodFlag.vue +36 -0
- package/packages/storefront/src/lib/components/SocialNetworkIcon.vue +1 -0
- package/packages/storefront/src/lib/components/globals/ALink.vue +5 -3
- package/packages/storefront/src/lib/composables/use-sticky-header.ts +1 -1
- package/packages/storefront/src/lib/layouts/BaseHead.astro +1 -1
- package/packages/storefront/src/lib/layouts/use-page-header.ts +2 -9
- package/packages/storefront/src/lib/ssr-context.ts +31 -25
- package/packages/storefront/src/vue-globals.d.ts +0 -1
- package/packages/types/index.ts +16 -0
- package/packages/types/package.json +1 -1
- package/packages/storefront/dist/server/chunks/endpoint@_@js.b79eed3b.mjs +0 -34
- package/packages/storefront/dist/server/chunks/pages/endpoint.js.4c130895.mjs +0 -119
- /package/ecomplus-stores/barra-doce/functions/ssr/src/pages/{fallback.astro → ~fallback.astro} +0 -0
- /package/ecomplus-stores/iluminim/functions/ssr/src/pages/{fallback.astro → ~fallback.astro} +0 -0
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const HEADER_CACHE_CONTROL = 'Cache-Control';
|
|
4
4
|
const HEADER_SSR_TOOK = 'X-Load-Took';
|
|
5
5
|
const HEADER_STALE_AT = 'X-Edge-Stale-At';
|
|
6
|
-
const HEADER_STYLE_LINK = 'X-Style-Link';
|
|
7
6
|
const resolveCacheControl = (response) => {
|
|
8
7
|
const cacheControl = response.headers.get(HEADER_CACHE_CONTROL);
|
|
9
8
|
if (!cacheControl || !response.headers.get(HEADER_SSR_TOOK)) {
|
|
@@ -54,23 +53,92 @@ const toCacheRes = (response, cacheControl, staleAt) => {
|
|
|
54
53
|
vary: null,
|
|
55
54
|
});
|
|
56
55
|
};
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
const checkToKvCache = (newCacheRes) => {
|
|
57
|
+
return newCacheRes.status === 200
|
|
58
|
+
&& newCacheRes.headers.get('Content-Type')?.includes('text/html');
|
|
59
|
+
};
|
|
60
|
+
const putKvCache = async (kv, kvKey, newCacheRes) => {
|
|
61
|
+
const newKvRes = new Response(newCacheRes.clone().body, { status: 200 });
|
|
62
|
+
const body = await newKvRes.text();
|
|
63
|
+
const headers = {};
|
|
64
|
+
newCacheRes.headers.forEach((value, key) => {
|
|
65
|
+
headers[key] = value;
|
|
66
|
+
});
|
|
67
|
+
const kvValue = JSON.stringify({ body, headers });
|
|
68
|
+
return kv.put(kvKey, kvValue, { expirationTtl: 3600 * 24 * 30 });
|
|
69
|
+
};
|
|
70
|
+
const swr = async (_request, env, ctx) => {
|
|
71
|
+
const url = new URL(_request.url);
|
|
72
|
+
const { hostname, pathname } = url;
|
|
73
|
+
const hostOverride = env[`OVERRIDE_${hostname}`];
|
|
74
|
+
if (hostOverride) {
|
|
75
|
+
url.hostname = hostOverride;
|
|
76
|
+
}
|
|
77
|
+
const bypassEarly = () => {
|
|
78
|
+
return !hostOverride
|
|
79
|
+
? fetch(_request)
|
|
80
|
+
: fetch(new Request(url.href, _request));
|
|
81
|
+
};
|
|
82
|
+
if (_request.method !== 'GET') {
|
|
83
|
+
return bypassEarly();
|
|
60
84
|
}
|
|
61
|
-
const { pathname } = new URL(event.request.url);
|
|
62
85
|
if (pathname === '/_image'
|
|
63
86
|
|| pathname.startsWith('/~')
|
|
64
87
|
|| pathname.startsWith('/api/')
|
|
65
88
|
|| pathname.startsWith('/_feeds/')) {
|
|
66
|
-
return
|
|
89
|
+
return bypassEarly();
|
|
67
90
|
}
|
|
68
|
-
const [uri] =
|
|
69
|
-
const request = new Request(`${uri}?t=${Date.now()}`,
|
|
70
|
-
const
|
|
71
|
-
|
|
91
|
+
const [uri] = url.href.split('?', 2);
|
|
92
|
+
const request = new Request(`${uri}?t=${Date.now()}`, _request);
|
|
93
|
+
const v = 38;
|
|
94
|
+
const cacheKey = new Request(`${uri}?v=${(v + 1)}`, {
|
|
95
|
+
method: _request.method,
|
|
72
96
|
});
|
|
73
|
-
const
|
|
97
|
+
const kvKey = `${v}${uri.replace('https:/', '')}`;
|
|
98
|
+
const kv = env.PERMA_CACHE;
|
|
99
|
+
let cachedRes;
|
|
100
|
+
let edgeSource = '';
|
|
101
|
+
try {
|
|
102
|
+
const gettingCache = caches.default.match(cacheKey);
|
|
103
|
+
let kvStoredRes;
|
|
104
|
+
const gettingKv = kv
|
|
105
|
+
? kv.get(kvKey, { type: 'json' }).then((value) => {
|
|
106
|
+
if (value) {
|
|
107
|
+
const { body, headers } = value;
|
|
108
|
+
kvStoredRes = new Response(body, { headers, status: 200 });
|
|
109
|
+
return kvStoredRes;
|
|
110
|
+
}
|
|
111
|
+
kvStoredRes = null;
|
|
112
|
+
return undefined;
|
|
113
|
+
})
|
|
114
|
+
: Promise.resolve(undefined);
|
|
115
|
+
cachedRes = await Promise.race([
|
|
116
|
+
new Promise((resolve) => {
|
|
117
|
+
gettingCache.then((fromCache) => {
|
|
118
|
+
if (fromCache) {
|
|
119
|
+
edgeSource = 'cache';
|
|
120
|
+
resolve(fromCache);
|
|
121
|
+
} else if (kvStoredRes !== undefined) {
|
|
122
|
+
resolve(kvStoredRes);
|
|
123
|
+
} else {
|
|
124
|
+
gettingKv.finally(() => resolve(kvStoredRes));
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}),
|
|
128
|
+
new Promise((resolve) => {
|
|
129
|
+
gettingKv.then((fromKv) => {
|
|
130
|
+
if (fromKv) {
|
|
131
|
+
edgeSource = 'kv';
|
|
132
|
+
resolve(fromKv);
|
|
133
|
+
} else {
|
|
134
|
+
gettingCache.then((fromCache) => resolve(fromCache));
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}),
|
|
138
|
+
]);
|
|
139
|
+
} catch {
|
|
140
|
+
//
|
|
141
|
+
}
|
|
74
142
|
let edgeState = 'miss';
|
|
75
143
|
if (cachedRes) {
|
|
76
144
|
const cachedStaleAt = Number(cachedRes.headers.get(HEADER_STALE_AT));
|
|
@@ -78,8 +146,11 @@ const swr = async (event) => {
|
|
|
78
146
|
edgeState = 'bypass';
|
|
79
147
|
} else if (Date.now() > cachedStaleAt) {
|
|
80
148
|
edgeState = 'stale';
|
|
81
|
-
|
|
149
|
+
ctx.waitUntil((async () => {
|
|
82
150
|
const newCacheRes = toCacheRes(await fetch(request));
|
|
151
|
+
if (kv && checkToKvCache(newCacheRes)) {
|
|
152
|
+
ctx.waitUntil(putKvCache(kv, kvKey, newCacheRes));
|
|
153
|
+
}
|
|
83
154
|
return caches.default.put(cacheKey, newCacheRes);
|
|
84
155
|
})());
|
|
85
156
|
} else {
|
|
@@ -90,21 +161,18 @@ const swr = async (event) => {
|
|
|
90
161
|
const { cacheControl, staleAt } = resolveCacheControl(response);
|
|
91
162
|
if (!cachedRes && response.ok) {
|
|
92
163
|
const newCacheRes = toCacheRes(response, cacheControl, staleAt);
|
|
93
|
-
|
|
164
|
+
ctx.waitUntil(caches.default.put(cacheKey, newCacheRes));
|
|
165
|
+
if (kv && checkToKvCache(newCacheRes)) {
|
|
166
|
+
ctx.waitUntil(putKvCache(kv, kvKey, newCacheRes));
|
|
167
|
+
}
|
|
94
168
|
}
|
|
95
|
-
|
|
169
|
+
return addHeaders(response, {
|
|
96
170
|
[HEADER_CACHE_CONTROL]: cacheControl,
|
|
97
171
|
'x-edge-state': edgeState,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
return addHeaders(response, headers);
|
|
172
|
+
'x-edge-src': edgeSource,
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export default {
|
|
177
|
+
fetch: swr,
|
|
106
178
|
};
|
|
107
|
-
// eslint-disable-next-line no-restricted-globals
|
|
108
|
-
addEventListener('fetch', (event) => {
|
|
109
|
-
event.respondWith(swr(event));
|
|
110
|
-
});
|
|
@@ -6,22 +6,24 @@ const { STOREFRONT_BASE_DIR, SSR_PROXY_DEBUG, SSR_PROXY_TIMEOUT } = process.env;
|
|
|
6
6
|
const baseDir = STOREFRONT_BASE_DIR || process.cwd();
|
|
7
7
|
let imagesManifest;
|
|
8
8
|
const builtImages = [];
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
let cssFilepath;
|
|
10
|
+
readFile(joinPath(baseDir, 'dist/server/stylesheets.csv'), 'utf-8')
|
|
11
|
+
.then((stylesManifest) => {
|
|
12
|
+
const cssFiles = [];
|
|
13
|
+
stylesManifest.split(/\n/).forEach((line) => {
|
|
14
|
+
const [filename] = line.split(',');
|
|
15
|
+
if (filename) cssFiles.push(filename);
|
|
16
|
+
});
|
|
17
|
+
if (cssFiles.length === 1) {
|
|
18
|
+
cssFilepath = cssFiles[0]?.replace('./dist/client/', '/');
|
|
19
|
+
if (cssFilepath
|
|
20
|
+
&& cssFilepath.charAt(0) !== '/'
|
|
21
|
+
&& !cssFilepath.startsWith('https://')) {
|
|
22
|
+
cssFilepath = `/${cssFilepath}`;
|
|
20
23
|
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
.catch(logger.warn);
|
|
25
27
|
const isProxyDebug = SSR_PROXY_DEBUG ? String(SSR_PROXY_DEBUG).toLowerCase() === 'true' : false;
|
|
26
28
|
const proxyTimeout = SSR_PROXY_TIMEOUT ? Number(SSR_PROXY_TIMEOUT) : 3000;
|
|
27
29
|
const proxy = async (req, res) => {
|
|
@@ -116,11 +118,11 @@ export default async (req, res) => {
|
|
|
116
118
|
.set('Cache-Control', cacheControl);
|
|
117
119
|
};
|
|
118
120
|
const fallback = (err, status = 500) => {
|
|
119
|
-
if (url !== '
|
|
121
|
+
if (url !== '/~fallback' && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
|
|
120
122
|
setStatusAndCache(status, 'public, max-age=120')
|
|
121
123
|
.send('<html><head>'
|
|
122
124
|
+ '<meta http-equiv="refresh" content="0; '
|
|
123
|
-
+ `url
|
|
125
|
+
+ `url=/~fallback?status=${status}&url=${encodeURIComponent(url)}"/>`
|
|
124
126
|
+ `</head><body>${err.toString()}</body></html>`);
|
|
125
127
|
} else {
|
|
126
128
|
setStatusAndCache(status, 'public, max-age=120, s-maxage=600')
|
|
@@ -167,15 +169,6 @@ export default async (req, res) => {
|
|
|
167
169
|
res.sendStatus(400);
|
|
168
170
|
return;
|
|
169
171
|
}
|
|
170
|
-
(async () => {
|
|
171
|
-
await readingStylesManifest;
|
|
172
|
-
if (cssFilename && !res.headersSent) {
|
|
173
|
-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/103 :zap:
|
|
174
|
-
res.writeEarlyHints({
|
|
175
|
-
link: `</${cssFilename}>; rel=preload; as=style`,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
})();
|
|
179
172
|
/*
|
|
180
173
|
Check Response methods used by Astro Node.js integration:
|
|
181
174
|
https://github.com/withastro/astro/blob/main/packages/integrations/node/src/nodeMiddleware.ts
|
|
@@ -184,8 +177,9 @@ export default async (req, res) => {
|
|
|
184
177
|
/* eslint-disable prefer-rest-params */
|
|
185
178
|
// @ts-ignore
|
|
186
179
|
res.writeHead = function writeHead(status, headers) {
|
|
187
|
-
if (status === 200 && headers &&
|
|
188
|
-
|
|
180
|
+
if (status === 200 && headers && cssFilepath) {
|
|
181
|
+
// https://community.cloudflare.com/t/early-hints-need-more-data-before-switching-over/342888/21
|
|
182
|
+
headers.Link = `<${cssFilepath}>; rel=preload; as=style`;
|
|
189
183
|
}
|
|
190
184
|
_writeHead.apply(res, [status, headers]);
|
|
191
185
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve-storefront.js","sourceRoot":"","sources":["../../src/firebase/serve-storefront.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAW/C,MAAM,EACJ,mBAAmB,EACnB,eAAe,EACf,iBAAiB,GAClB,GAAG,OAAO,CAAC,GAAG,CAAC;AAEhB,MAAM,OAAO,GAAG,mBAAmB,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;AACrD,IAAI,cAAsB,CAAC;AAE3B,MAAM,WAAW,GAAiB,EAAE,CAAC;AAErC,IAAI,WAA+B,CAAC;AACpC,
|
|
1
|
+
{"version":3,"file":"serve-storefront.js","sourceRoot":"","sources":["../../src/firebase/serve-storefront.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAW/C,MAAM,EACJ,mBAAmB,EACnB,eAAe,EACf,iBAAiB,GAClB,GAAG,OAAO,CAAC,GAAG,CAAC;AAEhB,MAAM,OAAO,GAAG,mBAAmB,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;AACrD,IAAI,cAAsB,CAAC;AAE3B,MAAM,WAAW,GAAiB,EAAE,CAAC;AAErC,IAAI,WAA+B,CAAC;AACpC,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,6BAA6B,CAAC,EAAE,OAAO,CAAC;KAChE,IAAI,CAAC,CAAC,cAAc,EAAE,EAAE;IACvB,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC1C,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC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ACnB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAClB,GAAG,CAAC,GAAG,EAAE,CAAC;gBACV,OAAO;aACR;YACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACjB,GAAG,CAAC,GAAG,CAAC,aAAa,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;YACpC,GAAG,CAAC,GAAG,CAAC,mBAAmB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;YACxC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACd,OAAO;SACR;QACD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/E,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;QAC7C,IAAI;YACF,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,KAAK,CAAC,CAAC;YACnC,iBAAiB,CAAC,GAAG,EAAE,kCAAkC,CAAC;iBACvD,IAAI,CAAC,IAAI,CAAC,CAAC;SACf;QAAC,OAAO,CAAC,EAAE;YACV,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAClB;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcommerce/ssr",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.24.0",
|
|
5
5
|
"description": "E-Com Plus Cloud Commerce storefront SSR",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -24,21 +24,21 @@
|
|
|
24
24
|
"build:workers": "npx tsc -p src/cloudflare --outDir cloudflare && npx eslint --ext .js cloudflare --fix"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@astrojs/node": "5.3.
|
|
27
|
+
"@astrojs/node": "5.3.4",
|
|
28
28
|
"@cloudcommerce/api": "workspace:*",
|
|
29
29
|
"@cloudcommerce/firebase": "workspace:*",
|
|
30
30
|
"@cloudcommerce/i18n": "workspace:*",
|
|
31
31
|
"@ecomplus/utils": "1.5.0-rc.5",
|
|
32
32
|
"@headlessui/vue": "^1.7.15",
|
|
33
33
|
"@vueuse/core": "10.3.0",
|
|
34
|
-
"astro": "2.10.
|
|
34
|
+
"astro": "2.10.9",
|
|
35
35
|
"firebase-admin": "^11.10.1",
|
|
36
36
|
"firebase-functions": "^4.4.1",
|
|
37
37
|
"vue": "^3.3.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@cloudcommerce/types": "workspace:*",
|
|
41
|
-
"@cloudflare/workers-types": "^4.
|
|
41
|
+
"@cloudflare/workers-types": "^4.20230807.0",
|
|
42
42
|
"@firebase/app-types": "^0.9.0"
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
const HEADER_CACHE_CONTROL = 'Cache-Control';
|
|
5
5
|
const HEADER_SSR_TOOK = 'X-Load-Took';
|
|
6
6
|
const HEADER_STALE_AT = 'X-Edge-Stale-At';
|
|
7
|
-
const HEADER_STYLE_LINK = 'X-Style-Link';
|
|
8
7
|
|
|
9
8
|
interface ParsedCacheControl {
|
|
10
9
|
'max-age'?: number;
|
|
@@ -69,25 +68,100 @@ const toCacheRes = (response: Response, cacheControl?: string | null, staleAt?:
|
|
|
69
68
|
});
|
|
70
69
|
};
|
|
71
70
|
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
const checkToKvCache = (newCacheRes: Response) => {
|
|
72
|
+
return newCacheRes.status === 200
|
|
73
|
+
&& newCacheRes.headers.get('Content-Type')?.includes('text/html');
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const putKvCache = async (kv: KVNamespace, kvKey: string, newCacheRes: Response) => {
|
|
77
|
+
const newKvRes = new Response(newCacheRes.clone().body, { status: 200 });
|
|
78
|
+
const body = await newKvRes.text();
|
|
79
|
+
const headers: Record<string, string> = {};
|
|
80
|
+
newCacheRes.headers.forEach((value, key) => {
|
|
81
|
+
headers[key] = value;
|
|
82
|
+
});
|
|
83
|
+
const kvValue = JSON.stringify({ body, headers });
|
|
84
|
+
return kv.put(kvKey, kvValue, { expirationTtl: 3600 * 24 * 30 });
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type Env = Record<`OVERRIDE_${string}`, string | undefined> & {
|
|
88
|
+
PERMA_CACHE: KVNamespace | undefined;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const swr = async (_request: Request, env: Env, ctx: ExecutionContext) => {
|
|
92
|
+
const url = new URL(_request.url);
|
|
93
|
+
const { hostname, pathname } = url;
|
|
94
|
+
const hostOverride = env[`OVERRIDE_${hostname}`];
|
|
95
|
+
if (hostOverride) {
|
|
96
|
+
url.hostname = hostOverride;
|
|
97
|
+
}
|
|
98
|
+
const bypassEarly = () => {
|
|
99
|
+
return !hostOverride
|
|
100
|
+
? fetch(_request)
|
|
101
|
+
: fetch(new Request(url.href, _request));
|
|
102
|
+
};
|
|
103
|
+
if (_request.method !== 'GET') {
|
|
104
|
+
return bypassEarly();
|
|
75
105
|
}
|
|
76
|
-
const { pathname } = new URL(event.request.url);
|
|
77
106
|
if (
|
|
78
107
|
pathname === '/_image'
|
|
79
108
|
|| pathname.startsWith('/~')
|
|
80
109
|
|| pathname.startsWith('/api/')
|
|
81
110
|
|| pathname.startsWith('/_feeds/')
|
|
82
111
|
) {
|
|
83
|
-
return
|
|
112
|
+
return bypassEarly();
|
|
84
113
|
}
|
|
85
|
-
const [uri] =
|
|
86
|
-
const request = new Request(`${uri}?t=${Date.now()}`,
|
|
87
|
-
const
|
|
88
|
-
|
|
114
|
+
const [uri] = url.href.split('?', 2);
|
|
115
|
+
const request = new Request(`${uri}?t=${Date.now()}`, _request);
|
|
116
|
+
const v = 38;
|
|
117
|
+
const cacheKey = new Request(`${uri}?v=${(v + 1)}`, {
|
|
118
|
+
method: _request.method,
|
|
89
119
|
});
|
|
90
|
-
const
|
|
120
|
+
const kvKey = `${v}${uri.replace('https:/', '')}`;
|
|
121
|
+
const kv = env.PERMA_CACHE;
|
|
122
|
+
let cachedRes: Response | null | undefined;
|
|
123
|
+
let edgeSource = '';
|
|
124
|
+
try {
|
|
125
|
+
const gettingCache = caches.default.match(cacheKey);
|
|
126
|
+
let kvStoredRes: typeof cachedRes;
|
|
127
|
+
const gettingKv = kv
|
|
128
|
+
? kv.get(kvKey, { type: 'json' }).then((value: any) => {
|
|
129
|
+
if (value) {
|
|
130
|
+
const { body, headers } = value as { body: string, headers: Record<string, string> };
|
|
131
|
+
kvStoredRes = new Response(body, { headers, status: 200 });
|
|
132
|
+
return kvStoredRes;
|
|
133
|
+
}
|
|
134
|
+
kvStoredRes = null;
|
|
135
|
+
return undefined;
|
|
136
|
+
})
|
|
137
|
+
: Promise.resolve(undefined);
|
|
138
|
+
cachedRes = await Promise.race([
|
|
139
|
+
new Promise((resolve) => {
|
|
140
|
+
gettingCache.then((fromCache) => {
|
|
141
|
+
if (fromCache) {
|
|
142
|
+
edgeSource = 'cache';
|
|
143
|
+
resolve(fromCache);
|
|
144
|
+
} else if (kvStoredRes !== undefined) {
|
|
145
|
+
resolve(kvStoredRes);
|
|
146
|
+
} else {
|
|
147
|
+
gettingKv.finally(() => resolve(kvStoredRes));
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}) as Promise<typeof cachedRes>,
|
|
151
|
+
new Promise((resolve) => {
|
|
152
|
+
gettingKv.then((fromKv) => {
|
|
153
|
+
if (fromKv) {
|
|
154
|
+
edgeSource = 'kv';
|
|
155
|
+
resolve(fromKv);
|
|
156
|
+
} else {
|
|
157
|
+
gettingCache.then((fromCache) => resolve(fromCache));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}) as Promise<typeof cachedRes>,
|
|
161
|
+
]);
|
|
162
|
+
} catch {
|
|
163
|
+
//
|
|
164
|
+
}
|
|
91
165
|
let edgeState = 'miss';
|
|
92
166
|
if (cachedRes) {
|
|
93
167
|
const cachedStaleAt = Number(cachedRes.headers.get(HEADER_STALE_AT));
|
|
@@ -95,8 +169,11 @@ const swr = async (event: FetchEvent) => {
|
|
|
95
169
|
edgeState = 'bypass';
|
|
96
170
|
} else if (Date.now() > cachedStaleAt) {
|
|
97
171
|
edgeState = 'stale';
|
|
98
|
-
|
|
172
|
+
ctx.waitUntil((async () => {
|
|
99
173
|
const newCacheRes = toCacheRes(await fetch(request));
|
|
174
|
+
if (kv && checkToKvCache(newCacheRes)) {
|
|
175
|
+
ctx.waitUntil(putKvCache(kv, kvKey, newCacheRes));
|
|
176
|
+
}
|
|
100
177
|
return caches.default.put(cacheKey, newCacheRes);
|
|
101
178
|
})());
|
|
102
179
|
} else {
|
|
@@ -107,22 +184,18 @@ const swr = async (event: FetchEvent) => {
|
|
|
107
184
|
const { cacheControl, staleAt } = resolveCacheControl(response);
|
|
108
185
|
if (!cachedRes && response.ok) {
|
|
109
186
|
const newCacheRes = toCacheRes(response, cacheControl, staleAt);
|
|
110
|
-
|
|
187
|
+
ctx.waitUntil(caches.default.put(cacheKey, newCacheRes));
|
|
188
|
+
if (kv && checkToKvCache(newCacheRes)) {
|
|
189
|
+
ctx.waitUntil(putKvCache(kv, kvKey, newCacheRes));
|
|
190
|
+
}
|
|
111
191
|
}
|
|
112
|
-
|
|
192
|
+
return addHeaders(response, {
|
|
113
193
|
[HEADER_CACHE_CONTROL]: cacheControl,
|
|
114
194
|
'x-edge-state': edgeState,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (styleLink) {
|
|
118
|
-
// https://developers.cloudflare.com/workers/examples/103-early-hints/
|
|
119
|
-
headers.link = `</${styleLink}>; rel=preload; as=style`;
|
|
120
|
-
headers[HEADER_STYLE_LINK] = null;
|
|
121
|
-
}
|
|
122
|
-
return addHeaders(response, headers);
|
|
195
|
+
'x-edge-src': edgeSource,
|
|
196
|
+
});
|
|
123
197
|
};
|
|
124
198
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
199
|
+
export default {
|
|
200
|
+
fetch: swr,
|
|
201
|
+
};
|
|
@@ -24,22 +24,26 @@ let imagesManifest: string;
|
|
|
24
24
|
type BuiltImage = { filename: string, width: number, height: number };
|
|
25
25
|
const builtImages: BuiltImage[] = [];
|
|
26
26
|
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
let cssFilepath: string | undefined;
|
|
28
|
+
readFile(joinPath(baseDir, 'dist/server/stylesheets.csv'), 'utf-8')
|
|
29
|
+
.then((stylesManifest) => {
|
|
30
|
+
const cssFiles: string[] = [];
|
|
31
|
+
stylesManifest.split(/\n/).forEach((line) => {
|
|
32
|
+
const [filename] = line.split(',');
|
|
33
|
+
if (filename) cssFiles.push(filename);
|
|
34
|
+
});
|
|
35
|
+
if (cssFiles.length === 1) {
|
|
36
|
+
cssFilepath = cssFiles[0]?.replace('./dist/client/', '/');
|
|
37
|
+
if (
|
|
38
|
+
cssFilepath
|
|
39
|
+
&& cssFilepath.charAt(0) !== '/'
|
|
40
|
+
&& !cssFilepath.startsWith('https://')
|
|
41
|
+
) {
|
|
42
|
+
cssFilepath = `/${cssFilepath}`;
|
|
38
43
|
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch(logger.warn);
|
|
43
47
|
|
|
44
48
|
const isProxyDebug = SSR_PROXY_DEBUG ? String(SSR_PROXY_DEBUG).toLowerCase() === 'true' : false;
|
|
45
49
|
const proxyTimeout = SSR_PROXY_TIMEOUT ? Number(SSR_PROXY_TIMEOUT) : 3000;
|
|
@@ -139,11 +143,11 @@ export default async (req: Request, res: Response) => {
|
|
|
139
143
|
};
|
|
140
144
|
|
|
141
145
|
const fallback = (err: any, status = 500) => {
|
|
142
|
-
if (url !== '
|
|
146
|
+
if (url !== '/~fallback' && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
|
|
143
147
|
setStatusAndCache(status, 'public, max-age=120')
|
|
144
148
|
.send('<html><head>'
|
|
145
149
|
+ '<meta http-equiv="refresh" content="0; '
|
|
146
|
-
+ `url
|
|
150
|
+
+ `url=/~fallback?status=${status}&url=${encodeURIComponent(url)}"/>`
|
|
147
151
|
+ `</head><body>${err.toString()}</body></html>`);
|
|
148
152
|
} else {
|
|
149
153
|
setStatusAndCache(status, 'public, max-age=120, s-maxage=600')
|
|
@@ -192,15 +196,6 @@ export default async (req: Request, res: Response) => {
|
|
|
192
196
|
return;
|
|
193
197
|
}
|
|
194
198
|
|
|
195
|
-
(async () => {
|
|
196
|
-
await readingStylesManifest;
|
|
197
|
-
if (cssFilename && !res.headersSent) {
|
|
198
|
-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/103 :zap:
|
|
199
|
-
res.writeEarlyHints({
|
|
200
|
-
link: `</${cssFilename}>; rel=preload; as=style`,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
})();
|
|
204
199
|
/*
|
|
205
200
|
Check Response methods used by Astro Node.js integration:
|
|
206
201
|
https://github.com/withastro/astro/blob/main/packages/integrations/node/src/nodeMiddleware.ts
|
|
@@ -209,8 +204,9 @@ export default async (req: Request, res: Response) => {
|
|
|
209
204
|
/* eslint-disable prefer-rest-params */
|
|
210
205
|
// @ts-ignore
|
|
211
206
|
res.writeHead = function writeHead(status: number, headers: OutgoingHttpHeaders) {
|
|
212
|
-
if (status === 200 && headers &&
|
|
213
|
-
|
|
207
|
+
if (status === 200 && headers && cssFilepath) {
|
|
208
|
+
// https://community.cloudflare.com/t/early-hints-need-more-data-before-switching-over/342888/21
|
|
209
|
+
headers.Link = `<${cssFilepath}>; rel=preload; as=style`;
|
|
214
210
|
}
|
|
215
211
|
_writeHead.apply(res, [status, headers]);
|
|
216
212
|
};
|