cloudcommerce 0.0.72 → 0.0.73

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/package.json +1 -1
  3. package/packages/api/package.json +1 -1
  4. package/packages/apps/correios/package.json +1 -1
  5. package/packages/apps/custom-shipping/package.json +1 -1
  6. package/packages/apps/discounts/package.json +1 -1
  7. package/packages/apps/frenet/package.json +1 -1
  8. package/packages/apps/tiny-erp/package.json +1 -1
  9. package/packages/cli/package.json +1 -1
  10. package/packages/config/package.json +1 -1
  11. package/packages/events/package.json +1 -1
  12. package/packages/firebase/package.json +1 -1
  13. package/packages/modules/package.json +1 -1
  14. package/packages/passport/package.json +1 -1
  15. package/packages/ssr/lib/firebase/serve-storefront.js +13 -32
  16. package/packages/ssr/lib/firebase/serve-storefront.js.map +1 -1
  17. package/packages/ssr/package.json +1 -1
  18. package/packages/ssr/src/firebase/serve-storefront.ts +13 -37
  19. package/packages/storefront/dist/client/assets/{404-500-_...d4aa8aff.css → _...d4aa8aff.css} +0 -0
  20. package/packages/storefront/dist/client/assets/{404-500-index.d9230d24.css → fallback-index.d9230d24.css} +0 -0
  21. package/packages/storefront/dist/client/assets/{404.b3ead908.css → fallback.9745a8aa.css} +1 -1
  22. package/packages/storefront/dist/server/entry.mjs +146 -174
  23. package/packages/storefront/package.json +1 -1
  24. package/packages/storefront/src/lib/ssr-context.ts +54 -30
  25. package/packages/storefront/src/lib/views/{404.astro → fallback.astro} +1 -1
  26. package/packages/storefront/src/pages/[...slug].astro +7 -2
  27. package/packages/storefront/src/pages/{404.astro → fallback.astro} +2 -2
  28. package/packages/storefront/src/pages/index.astro +7 -2
  29. package/packages/types/package.json +1 -1
  30. package/packages/storefront/dist/client/assets/500.d5d7700b.css +0 -1
  31. package/packages/storefront/src/lib/views/500.astro +0 -79
  32. package/packages/storefront/src/pages/500.astro +0 -13
@@ -29,6 +29,15 @@ if (!globalThis.api_prefetch_endpoints) {
29
29
 
30
30
  type ApiPrefetchEndpoints = Array<ApiEndpoint>;
31
31
 
32
+ const setResponseCache = (Astro: AstroGlobal, maxAge: number, sMaxAge?: number) => {
33
+ const headerName = import.meta.env.PROD ? 'Cache-Control' : 'X-Cache-Control';
34
+ let cacheControl = `public, max-age=${maxAge}`;
35
+ if (sMaxAge) {
36
+ cacheControl += `, s-maxage=${sMaxAge}, stale-while-revalidate=86400`;
37
+ }
38
+ Astro.response.headers.set(headerName, cacheControl);
39
+ };
40
+
32
41
  const loadPageContext = async (Astro: AstroGlobal, {
33
42
  cmsCollection,
34
43
  apiPrefetchEndpoints = globalThis.api_prefetch_endpoints,
@@ -36,50 +45,65 @@ const loadPageContext = async (Astro: AstroGlobal, {
36
45
  cmsCollection?: string;
37
46
  apiPrefetchEndpoints?: ApiPrefetchEndpoints;
38
47
  } = {}) => {
48
+ const urlPath = Astro.url.pathname;
39
49
  const { slug } = Astro.params;
40
50
  const config = getConfig();
41
51
  let cmsContent: Record<string, any> | undefined;
42
52
  let apiResource: string | undefined;
43
53
  let apiDoc: Record<string, any> | undefined;
44
54
  const apiState: { [k: string]: Record<string, any> } = {};
55
+ const apiOptions = {
56
+ fetch,
57
+ isNoAuth: true,
58
+ };
59
+ const apiFetchings = [
60
+ null, // fetch by slug
61
+ ...apiPrefetchEndpoints.map((endpoint) => api.get(endpoint, apiOptions)),
62
+ ];
45
63
  if (slug) {
46
64
  if (cmsCollection) {
47
65
  cmsContent = config.cms(`${cmsCollection}/${slug}`);
48
66
  } else {
49
- const apiOptions = {
50
- fetch,
51
- isNoAuth: true,
52
- };
53
- const apiFetchings = [
54
- api.get(`slugs/${slug}`, apiOptions),
55
- ...apiPrefetchEndpoints.map((endpoint) => api.get(endpoint, apiOptions)),
56
- ];
57
- try {
58
- const [slugResponse, ...prefetchResponses] = await Promise.all(apiFetchings);
59
- apiResource = slugResponse.data.resource;
60
- apiDoc = slugResponse.data.doc;
61
- apiState[`${apiResource}/${apiDoc._id}`] = apiDoc;
62
- prefetchResponses.forEach(({ config: { endpoint }, data }) => {
63
- apiState[endpoint] = data;
64
- });
65
- } catch (e: any) {
66
- const error: ApiError = e;
67
- let toUrl: string;
68
- if (error.statusCode === 404) {
69
- toUrl = '/404';
70
- Astro.response.headers.set('Cache-Control', 'public, max-age=120');
71
- } else {
72
- console.error(error);
73
- toUrl = '/500';
74
- Astro.response.headers.set('X-SSR-Error', error.stack);
75
- }
76
- const err: any = new Error(`Load page context failed: ${error.message}`);
77
- err.originalError = error;
78
- err.redirectUrl = `${toUrl}?url=${encodeURIComponent(Astro.url.pathname)}`;
67
+ apiFetchings[0] = api.get(`slugs/${slug}`, apiOptions);
68
+ }
69
+ }
70
+ try {
71
+ const [slugResponse, ...prefetchResponses] = await Promise.all(apiFetchings);
72
+ if (slugResponse) {
73
+ apiResource = slugResponse.data.resource;
74
+ apiDoc = slugResponse.data.doc;
75
+ apiState[`${apiResource}/${apiDoc._id}`] = apiDoc;
76
+ }
77
+ prefetchResponses.forEach(({ config: { endpoint }, data }) => {
78
+ apiState[endpoint] = data;
79
+ });
80
+ } catch (err: any) {
81
+ const error: ApiError = err;
82
+ const status = error.statusCode || 500;
83
+ if (status === 404) {
84
+ if (urlPath.endsWith('/')) {
85
+ err.redirectUrl = urlPath.slice(0, -1);
79
86
  err.astroResponse = Astro.redirect(err.redirectUrl);
80
87
  throw err;
81
88
  }
89
+ setResponseCache(Astro, 120, 300);
90
+ } else {
91
+ console.error(error);
92
+ setResponseCache(Astro, 30);
93
+ Astro.response.headers.set('X-SSR-Error', error.message);
82
94
  }
95
+ Astro.response.status = status;
96
+ err.responseHTML = `<html><head>
97
+ <meta http-equiv="refresh" content="0;
98
+ url=/fallback?status=${status}&url=${encodeURIComponent(urlPath)}"/>
99
+ </head>
100
+ <body></body></html>`;
101
+ throw err;
102
+ }
103
+ if (urlPath === '/fallback') {
104
+ setResponseCache(Astro, 3600, 86400);
105
+ } else {
106
+ setResponseCache(Astro, 120, 600);
83
107
  }
84
108
  return {
85
109
  ...config,
@@ -10,7 +10,7 @@ export interface Props {
10
10
 
11
11
  <Layout title="Welcome to Astro.">
12
12
  <main>
13
- <h1>404</h1>
13
+ <h1>Error :/</h1>
14
14
  <p class="instructions">
15
15
  Check out the <code>src/pages</code> directory to get started.<br/>
16
16
  <strong>Code Challenge:</strong> Tweak the "Welcome to Astro" message above.
@@ -3,11 +3,16 @@ import ViewWildcard from '../lib/views/[...slug].astro';
3
3
  import loadPageContext, { PageContext } from '../lib/ssr-context';
4
4
 
5
5
  let pageContext: PageContext;
6
+ let loadError: any;
6
7
  try {
7
8
  pageContext = await loadPageContext(Astro);
8
9
  } catch (err) {
9
- return err.astroResponse;
10
+ if (err.astroResponse) {
11
+ return err.astroResponse;
12
+ }
13
+ loadError = err;
10
14
  }
11
15
  ---
12
16
 
13
- <ViewWildcard pageContext={pageContext} />
17
+ {pageContext && <ViewWildcard pageContext={pageContext} />}
18
+ {loadError && <Fragment set:html={loadError.responseHTML} />}
@@ -1,5 +1,5 @@
1
1
  ---
2
- import View404 from '../lib/views/404.astro';
2
+ import ViewFallback from '../lib/views/fallback.astro';
3
3
  import loadPageContext, { PageContext } from '../lib/ssr-context';
4
4
 
5
5
  let pageContext: PageContext;
@@ -10,4 +10,4 @@ try {
10
10
  }
11
11
  ---
12
12
 
13
- <View404 pageContext={pageContext} />
13
+ <ViewFallback pageContext={pageContext} />
@@ -3,11 +3,16 @@ import ViewHome from '../lib/views/index.astro';
3
3
  import loadPageContext, { PageContext } from '../lib/ssr-context';
4
4
 
5
5
  let pageContext: PageContext;
6
+ let loadError: any;
6
7
  try {
7
8
  pageContext = await loadPageContext(Astro);
8
9
  } catch (err) {
9
- return err.astroResponse;
10
+ if (err.astroResponse) {
11
+ return err.astroResponse;
12
+ }
13
+ loadError = err;
10
14
  }
11
15
  ---
12
16
 
13
- <ViewHome pageContext={pageContext} />
17
+ {pageContext && <ViewHome pageContext={pageContext} />}
18
+ {loadError && <Fragment set:html={loadError.responseHTML} />}
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloudcommerce/types",
3
3
  "type": "module",
4
- "version": "0.0.72",
4
+ "version": "0.0.73",
5
5
  "description": "E-Com Plus Cloud Commerce reusable type definitions",
6
6
  "main": "index.ts",
7
7
  "repository": {
@@ -1 +0,0 @@
1
- :root{--astro-gradient: linear-gradient(0deg,#4F39FA, #DA62C4)}h1:where(.astro-NR6LLUHL){margin:2rem 0}main:where(.astro-NR6LLUHL){margin:auto;padding:1em;max-width:60ch}.text-gradient:where(.astro-NR6LLUHL){font-weight:900;background-image:var(--astro-gradient);-webkit-background-clip:text;-webkit-text-fill-color:transparent;background-size:100% 200%;background-position-y:100%;border-radius:.4rem;animation:pulse 4s ease-in-out infinite}@keyframes pulse{0%,to{background-position-y:0%}50%{background-position-y:80%}}.instructions:where(.astro-NR6LLUHL){line-height:1.6;margin:1rem 0;background:#4F39FA;padding:1rem;border-radius:.4rem;color:var(--color-bg)}.instructions:where(.astro-NR6LLUHL) code:where(.astro-NR6LLUHL){font-size:.875em;border:.1em solid var(--color-border);border-radius:4px;padding:.15em .25em}.link-card-grid:where(.astro-NR6LLUHL){display:grid;grid-template-columns:repeat(auto-fit,minmax(24ch,1fr));gap:1rem;padding:0}
@@ -1,79 +0,0 @@
1
- ---
2
- import type { PageContext } from '../ssr-context';
3
- import Layout from '../layouts/Layout.astro';
4
- import Card from '../components/Card.astro';
5
-
6
- export interface Props {
7
- pageContext: PageContext;
8
- }
9
- ---
10
-
11
- <Layout title="Welcome to Astro.">
12
- <main>
13
- <h1>5xx</h1>
14
- <p class="instructions">
15
- Check out the <code>src/pages</code> directory to get started.<br/>
16
- <strong>Code Challenge:</strong> Tweak the "Welcome to Astro" message above.
17
- </p>
18
- <ul role="list" class="link-card-grid">
19
- <Card href="https://docs.astro.build/" title="Documentation" body="Learn how Astro works and explore the official API docs." />
20
- <Card href="https://astro.build/integrations/" title="Integrations" body="Supercharge your project with new frameworks and libraries." />
21
- <Card href="https://astro.build/themes/" title="Themes" body="Explore a galaxy of community-built starter themes." />
22
- <Card href="https://astro.build/chat/" title="Chat" body="Come say hi to our amazing Discord community. ❤️" />
23
- </ul>
24
- </main>
25
- </Layout>
26
-
27
- <style>
28
- :root {
29
- --astro-gradient: linear-gradient(0deg,#4F39FA, #DA62C4);
30
- }
31
-
32
- h1 {
33
- margin: 2rem 0;
34
- }
35
-
36
- main {
37
- margin: auto;
38
- padding: 1em;
39
- max-width: 60ch;
40
- }
41
-
42
- .text-gradient {
43
- font-weight: 900;
44
- background-image: var(--astro-gradient);
45
- -webkit-background-clip: text;
46
- -webkit-text-fill-color: transparent;
47
- background-size: 100% 200%;
48
- background-position-y: 100%;
49
- border-radius: 0.4rem;
50
- animation: pulse 4s ease-in-out infinite;
51
- }
52
-
53
- @keyframes pulse {
54
- 0%, 100% { background-position-y: 0%; }
55
- 50% { background-position-y: 80%; }
56
- }
57
-
58
- .instructions {
59
- line-height: 1.6;
60
- margin: 1rem 0;
61
- background: #4F39FA;
62
- padding: 1.0rem;
63
- border-radius: 0.4rem;
64
- color: var(--color-bg);
65
- }
66
-
67
- .instructions code {
68
- font-size: 0.875em;
69
- border: 0.1em solid var(--color-border);
70
- border-radius: 4px;
71
- padding: 0.15em 0.25em;
72
- }
73
- .link-card-grid {
74
- display: grid;
75
- grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
76
- gap: 1rem;
77
- padding: 0;
78
- }
79
- </style>
@@ -1,13 +0,0 @@
1
- ---
2
- import View500 from '../lib/views/500.astro';
3
- import loadPageContext, { PageContext } from '../lib/ssr-context';
4
-
5
- let pageContext: PageContext;
6
- try {
7
- pageContext = await loadPageContext(Astro);
8
- } catch (err) {
9
- return err.astroResponse;
10
- }
11
- ---
12
-
13
- <View500 pageContext={pageContext} />