@quilted/quilt 0.5.158 → 0.6.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.
Files changed (171) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/build/esm/graphql/testing/matchers/operations.mjs +1 -1
  3. package/build/esm/graphql/testing/matchers/utilities.mjs +1 -1
  4. package/build/esm/graphql/testing/matchers.mjs +4 -3
  5. package/build/esm/graphql/testing.mjs +5 -1
  6. package/build/esm/react/testing.mjs +5 -2
  7. package/build/esnext/globals.esnext +1 -1
  8. package/build/esnext/graphql/testing/matchers/operations.esnext +49 -19
  9. package/build/esnext/graphql/testing/matchers/utilities.esnext +18 -15
  10. package/build/esnext/graphql/testing/matchers.esnext +4 -3
  11. package/build/esnext/graphql/testing.esnext +4 -1
  12. package/build/esnext/html/HTML.esnext +6 -9
  13. package/build/esnext/react/testing.esnext +4 -2
  14. package/build/esnext/server/ServerContext.esnext +14 -2
  15. package/build/esnext/server/preload.esnext +9 -10
  16. package/build/esnext/server/request-router.esnext +138 -141
  17. package/build/esnext/utilities/react.esnext +1 -4
  18. package/build/tsconfig.tsbuildinfo +1 -1
  19. package/build/typescript/globals.d.ts +1 -0
  20. package/build/typescript/globals.d.ts.map +1 -1
  21. package/build/typescript/graphql/testing/matchers/utilities.d.ts +1 -1
  22. package/build/typescript/graphql/testing/matchers/utilities.d.ts.map +1 -1
  23. package/build/typescript/graphql/testing/matchers.d.ts +10 -8
  24. package/build/typescript/graphql/testing/matchers.d.ts.map +1 -1
  25. package/build/typescript/graphql/testing.d.ts +7 -1
  26. package/build/typescript/graphql/testing.d.ts.map +1 -1
  27. package/build/typescript/modules.d.ts +23 -0
  28. package/build/typescript/modules.d.ts.map +1 -0
  29. package/build/typescript/react/testing.d.ts +7 -2
  30. package/build/typescript/react/testing.d.ts.map +1 -1
  31. package/configuration/rollup.config.js +3 -0
  32. package/package.json +66 -197
  33. package/source/globals.ts +2 -0
  34. package/source/graphql/testing/matchers/operations.ts +1 -1
  35. package/source/graphql/testing/matchers/utilities.ts +1 -1
  36. package/source/graphql/testing/matchers.ts +15 -22
  37. package/source/graphql/testing.ts +11 -1
  38. package/source/modules.ts +31 -0
  39. package/source/react/testing.ts +10 -3
  40. package/tsconfig.json +1 -3
  41. package/build/cjs/assets.cjs +0 -39
  42. package/build/cjs/async.cjs +0 -32
  43. package/build/cjs/env.cjs +0 -7
  44. package/build/cjs/events.cjs +0 -46
  45. package/build/cjs/globals.cjs +0 -21
  46. package/build/cjs/graphql/server.cjs +0 -14
  47. package/build/cjs/graphql/testing/matchers/operations.cjs +0 -42
  48. package/build/cjs/graphql/testing/matchers/utilities.cjs +0 -43
  49. package/build/cjs/graphql/testing/matchers.cjs +0 -10
  50. package/build/cjs/graphql/testing.cjs +0 -18
  51. package/build/cjs/graphql.cjs +0 -21
  52. package/build/cjs/html/HTML.cjs +0 -21
  53. package/build/cjs/html.cjs +0 -133
  54. package/build/cjs/http.cjs +0 -146
  55. package/build/cjs/index.cjs +0 -2
  56. package/build/cjs/localize.cjs +0 -46
  57. package/build/cjs/magic/app.cjs +0 -2
  58. package/build/cjs/magic/assets.cjs +0 -2
  59. package/build/cjs/magic/request-router.cjs +0 -2
  60. package/build/cjs/navigate/testing.cjs +0 -12
  61. package/build/cjs/navigate.cjs +0 -70
  62. package/build/cjs/performance.cjs +0 -26
  63. package/build/cjs/polyfills/abort-controller.cjs +0 -4
  64. package/build/cjs/polyfills/base.cjs +0 -4
  65. package/build/cjs/polyfills/crypto.cjs +0 -4
  66. package/build/cjs/polyfills/fetch-get-set-cookie.cjs +0 -4
  67. package/build/cjs/polyfills/fetch.cjs +0 -4
  68. package/build/cjs/polyfills/noop.cjs +0 -4
  69. package/build/cjs/react/testing.cjs +0 -30
  70. package/build/cjs/react/tools.cjs +0 -19
  71. package/build/cjs/react.cjs +0 -15
  72. package/build/cjs/request-router/index.cjs +0 -12
  73. package/build/cjs/request-router/node.cjs +0 -12
  74. package/build/cjs/server/ServerContext.cjs +0 -20
  75. package/build/cjs/server/preload.cjs +0 -37
  76. package/build/cjs/server/request-router.cjs +0 -266
  77. package/build/cjs/server.cjs +0 -114
  78. package/build/cjs/signals.cjs +0 -51
  79. package/build/cjs/static/StaticContext.cjs +0 -20
  80. package/build/cjs/static/index.cjs +0 -261
  81. package/build/cjs/static/render.cjs +0 -44
  82. package/build/cjs/testing.cjs +0 -12
  83. package/build/cjs/threads.cjs +0 -117
  84. package/build/cjs/utilities/react.cjs +0 -12
  85. package/build/esm/magic/request-router.mjs +0 -1
  86. package/build/esm/polyfills/abort-controller.mjs +0 -1
  87. package/build/esm/polyfills/base.mjs +0 -1
  88. package/build/esm/polyfills/crypto.mjs +0 -1
  89. package/build/esm/polyfills/fetch-get-set-cookie.mjs +0 -1
  90. package/build/esm/polyfills/fetch.mjs +0 -1
  91. package/build/esm/polyfills/noop.mjs +0 -1
  92. package/build/esm/static/StaticContext.mjs +0 -18
  93. package/build/esm/static/index.mjs +0 -259
  94. package/build/esm/static/render.mjs +0 -42
  95. package/build/esm/testing.mjs +0 -1
  96. package/build/esnext/magic/app.esnext +0 -1
  97. package/build/esnext/magic/assets.esnext +0 -1
  98. package/build/esnext/magic/request-router.esnext +0 -1
  99. package/build/esnext/polyfills/abort-controller.esnext +0 -1
  100. package/build/esnext/polyfills/base.esnext +0 -1
  101. package/build/esnext/polyfills/crypto.esnext +0 -1
  102. package/build/esnext/polyfills/fetch-get-set-cookie.esnext +0 -1
  103. package/build/esnext/polyfills/fetch.esnext +0 -1
  104. package/build/esnext/polyfills/noop.esnext +0 -1
  105. package/build/esnext/static/StaticContext.esnext +0 -18
  106. package/build/esnext/static/index.esnext +0 -259
  107. package/build/esnext/static/render.esnext +0 -42
  108. package/build/esnext/testing.esnext +0 -1
  109. package/build/typescript/App.d.ts +0 -48
  110. package/build/typescript/App.d.ts.map +0 -1
  111. package/build/typescript/TestApp.d.ts +0 -17
  112. package/build/typescript/TestApp.d.ts.map +0 -1
  113. package/build/typescript/global.d.ts +0 -8
  114. package/build/typescript/global.d.ts.map +0 -1
  115. package/build/typescript/magic/app.d.ts +0 -4
  116. package/build/typescript/magic/app.d.ts.map +0 -1
  117. package/build/typescript/magic/assets.d.ts +0 -5
  118. package/build/typescript/magic/assets.d.ts.map +0 -1
  119. package/build/typescript/magic/request-router.d.ts +0 -4
  120. package/build/typescript/magic/request-router.d.ts.map +0 -1
  121. package/build/typescript/matchers/graphql/operations.d.ts +0 -7
  122. package/build/typescript/matchers/graphql/operations.d.ts.map +0 -1
  123. package/build/typescript/matchers/graphql/utilities.d.ts +0 -8
  124. package/build/typescript/matchers/graphql/utilities.d.ts.map +0 -1
  125. package/build/typescript/matchers/graphql.d.ts +0 -9
  126. package/build/typescript/matchers/graphql.d.ts.map +0 -1
  127. package/build/typescript/matchers.d.ts +0 -3
  128. package/build/typescript/matchers.d.ts.map +0 -1
  129. package/build/typescript/polyfills/abort-controller.d.ts +0 -2
  130. package/build/typescript/polyfills/abort-controller.d.ts.map +0 -1
  131. package/build/typescript/polyfills/base.d.ts +0 -2
  132. package/build/typescript/polyfills/base.d.ts.map +0 -1
  133. package/build/typescript/polyfills/crypto.d.ts +0 -2
  134. package/build/typescript/polyfills/crypto.d.ts.map +0 -1
  135. package/build/typescript/polyfills/fetch-get-set-cookie.d.ts +0 -2
  136. package/build/typescript/polyfills/fetch-get-set-cookie.d.ts.map +0 -1
  137. package/build/typescript/polyfills/fetch.d.ts +0 -2
  138. package/build/typescript/polyfills/fetch.d.ts.map +0 -1
  139. package/build/typescript/polyfills/noop.d.ts +0 -2
  140. package/build/typescript/polyfills/noop.d.ts.map +0 -1
  141. package/build/typescript/react/index.d.ts +0 -3
  142. package/build/typescript/react/index.d.ts.map +0 -1
  143. package/build/typescript/react-dom/index.d.ts +0 -3
  144. package/build/typescript/react-dom/index.d.ts.map +0 -1
  145. package/build/typescript/server/index.d.ts +0 -16
  146. package/build/typescript/server/index.d.ts.map +0 -1
  147. package/build/typescript/static/StaticContext.d.ts +0 -13
  148. package/build/typescript/static/StaticContext.d.ts.map +0 -1
  149. package/build/typescript/static/index.d.ts +0 -20
  150. package/build/typescript/static/index.d.ts.map +0 -1
  151. package/build/typescript/static/render.d.ts +0 -19
  152. package/build/typescript/static/render.d.ts.map +0 -1
  153. package/build/typescript/testing.d.ts +0 -2
  154. package/build/typescript/testing.d.ts.map +0 -1
  155. package/quilt.project.ts +0 -5
  156. package/source/magic/app.ts +0 -5
  157. package/source/magic/assets.ts +0 -8
  158. package/source/magic/request-router.ts +0 -5
  159. package/source/polyfills/README.md +0 -3
  160. package/source/polyfills/abort-controller.ts +0 -1
  161. package/source/polyfills/base.ts +0 -1
  162. package/source/polyfills/crypto.ts +0 -1
  163. package/source/polyfills/fetch-get-set-cookie.ts +0 -1
  164. package/source/polyfills/fetch.ts +0 -1
  165. package/source/polyfills/noop.ts +0 -1
  166. package/source/static/StaticContext.tsx +0 -39
  167. package/source/static/index.tsx +0 -315
  168. package/source/static/render.tsx +0 -40
  169. package/source/testing.ts +0 -1
  170. /package/build/esm/{magic/app.mjs → modules.mjs} +0 -0
  171. /package/build/{esm/magic/assets.mjs → esnext/modules.esnext} +0 -0
@@ -1,259 +0,0 @@
1
- import { renderToStaticMarkup } from 'react-dom/server';
2
- import { Head, Script, Style, StylePreload, ScriptPreload } from '@quilted/react-html/server';
3
- import { StaticRenderer, StaticRendererContext } from '@quilted/react-router/static';
4
- import { renderApp } from './render.mjs';
5
- import { jsxs, jsx } from 'react/jsx-runtime';
6
-
7
- const BASE_URL = 'http://localhost:3000';
8
- async function renderStatic(App, {
9
- assets,
10
- routes: startingRoutes,
11
- onRender,
12
- crawl = true,
13
- baseUrl = BASE_URL,
14
- prettify = true
15
- }) {
16
- const routesToHandle = startingRoutes.map(route => ({
17
- route: removePostfixSlash(new URL(route, baseUrl).pathname),
18
- fallback: false
19
- }));
20
- const seenRoutes = [...routesToHandle];
21
- const seenRouteIds = new Set(seenRoutes.map(({
22
- route
23
- }) => route));
24
- let renderableRoute;
25
- while (renderableRoute = routesToHandle.shift()) {
26
- const {
27
- route,
28
- fallback
29
- } = renderableRoute;
30
- const url = new URL(route, baseUrl);
31
- const {
32
- html,
33
- http,
34
- routes
35
- } = await renderUrl(url, {
36
- fallback
37
- });
38
- if (crawl) {
39
- for (const {
40
- routes: routeDefinitions,
41
- fallback = false,
42
- consumedPath,
43
- prefix
44
- } of routes) {
45
- const basePathname = joinPath(prefix, consumedPath);
46
- const baseId = basePathname === '/' ? basePathname : `__QUILT_BASE_${basePathname}__`;
47
- for (const routeDefinition of routeDefinitions) {
48
- await recordRouteDefinition(routeDefinition, {
49
- baseId,
50
- basePathname,
51
- addFallbacks: fallback
52
- });
53
- }
54
- if (fallback && routeDefinitions[routeDefinitions.length - 1]?.match != null) {
55
- await recordRouteDefinition({}, {
56
- baseId,
57
- basePathname,
58
- addFallbacks: fallback
59
- });
60
- }
61
- }
62
- }
63
- await onRender({
64
- route,
65
- content: html,
66
- http,
67
- fallback,
68
- hasChildren: !fallback && seenRoutes.some(otherRoute => otherRoute.route.startsWith(`${route}/`))
69
- });
70
- }
71
- async function recordRouteDefinition({
72
- match,
73
- children,
74
- renderStatic
75
- }, {
76
- basePathname,
77
- baseId,
78
- addFallbacks
79
- }) {
80
- if (renderStatic === false) return;
81
- let routeId;
82
- const hasChildren = children && children.length > 0;
83
- const hasManualMatches = typeof renderStatic === 'function' && typeof match !== 'string';
84
- const matchedRoutes = [];
85
- if (typeof match === 'string') {
86
- routeId = joinPath(baseId, match);
87
- matchedRoutes.push({
88
- id: routeId,
89
- route: joinPath(basePathname, match),
90
- fallback: false
91
- });
92
- } else if (typeof match === 'function') {
93
- routeId = joinPath(baseId, `__QUILT_FUNCTION_ROUTE_${match.toString()}__`);
94
- } else if (match instanceof RegExp) {
95
- routeId = joinPath(`__QUILT_REGEX_ROUTE_${match.source}__`);
96
- } else {
97
- routeId = joinPath(baseId, '__QUILT_FALLBACK_ROUTE__');
98
- if (!hasManualMatches) {
99
- matchedRoutes.push({
100
- id: routeId,
101
- route: basePathname,
102
- fallback: true
103
- });
104
- }
105
- }
106
- if (seenRouteIds.has(routeId)) return;
107
- seenRouteIds.add(routeId);
108
- if (typeof renderStatic === 'function' && typeof match !== 'string') {
109
- const matchedRouteParts = await renderStatic();
110
- for (const routePart of matchedRouteParts) {
111
- const id = joinPath(baseId, `__QUILT_MATCH_${routePart}__`);
112
- seenRouteIds.add(id);
113
- matchedRoutes.push({
114
- id,
115
- route: joinPath(basePathname, routePart),
116
- fallback: false
117
- });
118
- }
119
- }
120
- if (hasChildren) {
121
- for (const {
122
- id,
123
- route
124
- } of matchedRoutes) {
125
- for (const child of children) {
126
- await recordRouteDefinition(child, {
127
- addFallbacks,
128
- basePathname: route,
129
- baseId: id
130
- });
131
- }
132
- if (addFallbacks && children[children.length - 1]?.match != null) {
133
- await recordRouteDefinition({}, {
134
- addFallbacks,
135
- basePathname: route,
136
- baseId: id
137
- });
138
- }
139
- }
140
- return;
141
- }
142
- for (const {
143
- route,
144
- fallback
145
- } of matchedRoutes) {
146
- const renderableRoute = {
147
- route,
148
- fallback
149
- };
150
- seenRoutes.push(renderableRoute);
151
- routesToHandle.push(renderableRoute);
152
- }
153
- }
154
- async function renderUrl(url, {
155
- fallback = false
156
- } = {}) {
157
- const routeRecorder = new StaticRenderer({
158
- forceFallback: fallback ? url.pathname : undefined
159
- });
160
- const initialCacheKey = await assets.cacheKey?.(new Request(url));
161
- const {
162
- http,
163
- html: htmlManager,
164
- assets: assetsManager,
165
- markup
166
- } = await renderApp( /*#__PURE__*/jsx(App, {}), {
167
- url,
168
- cacheKey: initialCacheKey,
169
- decorate(app) {
170
- return /*#__PURE__*/jsx(StaticRendererContext.Provider, {
171
- value: routeRecorder,
172
- children: app
173
- });
174
- }
175
- });
176
- const cacheKey = assetsManager.cacheKey;
177
- const [mainAssets, preloadAssets] = await Promise.all([assets.entry({
178
- modules: assetsManager.usedModules({
179
- timing: 'load'
180
- }),
181
- cacheKey
182
- }), assets.modules(assetsManager.usedModules({
183
- timing: 'preload'
184
- }), {
185
- cacheKey
186
- })]);
187
- const {
188
- htmlAttributes,
189
- bodyAttributes,
190
- ...headProps
191
- } = htmlManager.state;
192
- const minifiedHTML = '<!DOCTYPE html>' + renderToStaticMarkup(
193
-
194
- // eslint-disable-next-line jsx-a11y/html-has-lang
195
- jsxs("html", {
196
- ...htmlAttributes,
197
- children: [/*#__PURE__*/jsxs("head", {
198
- children: [/*#__PURE__*/jsx(Head, {
199
- ...headProps
200
- }), mainAssets?.scripts.map(script => /*#__PURE__*/jsx(Script, {
201
- asset: script,
202
- baseUrl: url
203
- }, script.source)), mainAssets?.styles.map(style => /*#__PURE__*/jsx(Style, {
204
- asset: style,
205
- baseUrl: url
206
- }, style.source)), preloadAssets?.styles.map(style => /*#__PURE__*/jsx(StylePreload, {
207
- asset: style,
208
- baseUrl: url
209
- }, style.source)), preloadAssets?.scripts.map(script => /*#__PURE__*/jsx(ScriptPreload, {
210
- asset: script,
211
- baseUrl: url
212
- }, script.source))]
213
- }), /*#__PURE__*/jsx("body", {
214
- ...bodyAttributes,
215
- children: /*#__PURE__*/jsx("div", {
216
- id: "app",
217
- dangerouslySetInnerHTML: {
218
- __html: markup ?? ''
219
- }
220
- })
221
- })]
222
- }));
223
- const html = prettify ? await prettifyHTML(minifiedHTML) : minifiedHTML;
224
- return {
225
- html,
226
- http: http.state,
227
- routes: routeRecorder.state
228
- };
229
- }
230
- }
231
- async function prettifyHTML(html) {
232
- try {
233
- const {
234
- default: prettier
235
- } = await import('prettier');
236
- return prettier.format(html, {
237
- parser: 'html'
238
- });
239
- } catch (error) {
240
- return html;
241
- }
242
- }
243
- function joinPath(...parts) {
244
- let path = '/';
245
- for (const part of parts) {
246
- if (typeof part !== 'string') continue;
247
- const normalizedPart = part.startsWith('/') ? part.slice(1) : part;
248
- if (normalizedPart.length === 0) continue;
249
- if (path !== '/') path += '/';
250
- path += normalizedPart;
251
- path = removePostfixSlash(path);
252
- }
253
- return path;
254
- }
255
- function removePostfixSlash(value) {
256
- return value.endsWith('/') && value !== '/' ? value.slice(0, value.length - 1) : value;
257
- }
258
-
259
- export { renderStatic };
@@ -1,42 +0,0 @@
1
- import { extract } from '@quilted/react-server-render/server';
2
- import { HTMLManager } from '@quilted/react-html/server';
3
- import { HttpManager } from '@quilted/react-http/server';
4
- import { AssetsManager } from '@quilted/react-assets/server';
5
- import { StaticContext } from './StaticContext.mjs';
6
- import { jsx } from 'react/jsx-runtime';
7
-
8
- async function renderApp(app, {
9
- decorate,
10
- url,
11
- headers,
12
- cacheKey,
13
- ...rest
14
- } = {}) {
15
- const html = new HTMLManager();
16
- const http = new HttpManager({
17
- headers
18
- });
19
- const assets = new AssetsManager({
20
- cacheKey
21
- });
22
- const markup = await extract(app, {
23
- decorate(app) {
24
- return /*#__PURE__*/jsx(StaticContext, {
25
- html: html,
26
- http: http,
27
- url: url,
28
- assets: assets,
29
- children: decorate?.(app) ?? app
30
- });
31
- },
32
- ...rest
33
- });
34
- return {
35
- markup,
36
- http,
37
- html,
38
- assets
39
- };
40
- }
41
-
42
- export { renderApp };
@@ -1 +0,0 @@
1
- export * from '@quilted/testing';
@@ -1 +0,0 @@
1
-
@@ -1 +0,0 @@
1
-
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/abort-controller';
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/base';
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/crypto';
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/fetch-get-set-cookie';
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/fetch';
@@ -1 +0,0 @@
1
- import '@quilted/polyfills/noop';
@@ -1,18 +0,0 @@
1
- import { AssetsContext } from '@quilted/react-assets/server';
2
- import { InitialUrlContext } from '@quilted/react-router';
3
- import { HTMLContext } from '@quilted/react-html/server';
4
- import { HttpServerContext } from '@quilted/react-http/server';
5
- import { maybeWrapContext } from '../utilities/react.esnext';
6
-
7
- function StaticContext({
8
- url,
9
- html,
10
- http,
11
- assets,
12
- children
13
- }) {
14
- const normalizedUrl = typeof url === 'string' ? new URL(url) : url;
15
- return maybeWrapContext(AssetsContext, assets, maybeWrapContext(HttpServerContext, http, maybeWrapContext(HTMLContext, html, maybeWrapContext(InitialUrlContext, normalizedUrl, children))));
16
- }
17
-
18
- export { StaticContext };
@@ -1,259 +0,0 @@
1
- import { renderToStaticMarkup } from 'react-dom/server';
2
- import { Head, Script, Style, StylePreload, ScriptPreload } from '@quilted/react-html/server';
3
- import { StaticRenderer, StaticRendererContext } from '@quilted/react-router/static';
4
- import { renderApp } from './render.esnext';
5
- import { jsxs, jsx } from 'react/jsx-runtime';
6
-
7
- const BASE_URL = 'http://localhost:3000';
8
- async function renderStatic(App, {
9
- assets,
10
- routes: startingRoutes,
11
- onRender,
12
- crawl = true,
13
- baseUrl = BASE_URL,
14
- prettify = true
15
- }) {
16
- const routesToHandle = startingRoutes.map(route => ({
17
- route: removePostfixSlash(new URL(route, baseUrl).pathname),
18
- fallback: false
19
- }));
20
- const seenRoutes = [...routesToHandle];
21
- const seenRouteIds = new Set(seenRoutes.map(({
22
- route
23
- }) => route));
24
- let renderableRoute;
25
- while (renderableRoute = routesToHandle.shift()) {
26
- const {
27
- route,
28
- fallback
29
- } = renderableRoute;
30
- const url = new URL(route, baseUrl);
31
- const {
32
- html,
33
- http,
34
- routes
35
- } = await renderUrl(url, {
36
- fallback
37
- });
38
- if (crawl) {
39
- for (const {
40
- routes: routeDefinitions,
41
- fallback = false,
42
- consumedPath,
43
- prefix
44
- } of routes) {
45
- const basePathname = joinPath(prefix, consumedPath);
46
- const baseId = basePathname === '/' ? basePathname : `__QUILT_BASE_${basePathname}__`;
47
- for (const routeDefinition of routeDefinitions) {
48
- await recordRouteDefinition(routeDefinition, {
49
- baseId,
50
- basePathname,
51
- addFallbacks: fallback
52
- });
53
- }
54
- if (fallback && routeDefinitions[routeDefinitions.length - 1]?.match != null) {
55
- await recordRouteDefinition({}, {
56
- baseId,
57
- basePathname,
58
- addFallbacks: fallback
59
- });
60
- }
61
- }
62
- }
63
- await onRender({
64
- route,
65
- content: html,
66
- http,
67
- fallback,
68
- hasChildren: !fallback && seenRoutes.some(otherRoute => otherRoute.route.startsWith(`${route}/`))
69
- });
70
- }
71
- async function recordRouteDefinition({
72
- match,
73
- children,
74
- renderStatic
75
- }, {
76
- basePathname,
77
- baseId,
78
- addFallbacks
79
- }) {
80
- if (renderStatic === false) return;
81
- let routeId;
82
- const hasChildren = children && children.length > 0;
83
- const hasManualMatches = typeof renderStatic === 'function' && typeof match !== 'string';
84
- const matchedRoutes = [];
85
- if (typeof match === 'string') {
86
- routeId = joinPath(baseId, match);
87
- matchedRoutes.push({
88
- id: routeId,
89
- route: joinPath(basePathname, match),
90
- fallback: false
91
- });
92
- } else if (typeof match === 'function') {
93
- routeId = joinPath(baseId, `__QUILT_FUNCTION_ROUTE_${match.toString()}__`);
94
- } else if (match instanceof RegExp) {
95
- routeId = joinPath(`__QUILT_REGEX_ROUTE_${match.source}__`);
96
- } else {
97
- routeId = joinPath(baseId, '__QUILT_FALLBACK_ROUTE__');
98
- if (!hasManualMatches) {
99
- matchedRoutes.push({
100
- id: routeId,
101
- route: basePathname,
102
- fallback: true
103
- });
104
- }
105
- }
106
- if (seenRouteIds.has(routeId)) return;
107
- seenRouteIds.add(routeId);
108
- if (typeof renderStatic === 'function' && typeof match !== 'string') {
109
- const matchedRouteParts = await renderStatic();
110
- for (const routePart of matchedRouteParts) {
111
- const id = joinPath(baseId, `__QUILT_MATCH_${routePart}__`);
112
- seenRouteIds.add(id);
113
- matchedRoutes.push({
114
- id,
115
- route: joinPath(basePathname, routePart),
116
- fallback: false
117
- });
118
- }
119
- }
120
- if (hasChildren) {
121
- for (const {
122
- id,
123
- route
124
- } of matchedRoutes) {
125
- for (const child of children) {
126
- await recordRouteDefinition(child, {
127
- addFallbacks,
128
- basePathname: route,
129
- baseId: id
130
- });
131
- }
132
- if (addFallbacks && children[children.length - 1]?.match != null) {
133
- await recordRouteDefinition({}, {
134
- addFallbacks,
135
- basePathname: route,
136
- baseId: id
137
- });
138
- }
139
- }
140
- return;
141
- }
142
- for (const {
143
- route,
144
- fallback
145
- } of matchedRoutes) {
146
- const renderableRoute = {
147
- route,
148
- fallback
149
- };
150
- seenRoutes.push(renderableRoute);
151
- routesToHandle.push(renderableRoute);
152
- }
153
- }
154
- async function renderUrl(url, {
155
- fallback = false
156
- } = {}) {
157
- const routeRecorder = new StaticRenderer({
158
- forceFallback: fallback ? url.pathname : undefined
159
- });
160
- const initialCacheKey = await assets.cacheKey?.(new Request(url));
161
- const {
162
- http,
163
- html: htmlManager,
164
- assets: assetsManager,
165
- markup
166
- } = await renderApp( /*#__PURE__*/jsx(App, {}), {
167
- url,
168
- cacheKey: initialCacheKey,
169
- decorate(app) {
170
- return /*#__PURE__*/jsx(StaticRendererContext.Provider, {
171
- value: routeRecorder,
172
- children: app
173
- });
174
- }
175
- });
176
- const cacheKey = assetsManager.cacheKey;
177
- const [mainAssets, preloadAssets] = await Promise.all([assets.entry({
178
- modules: assetsManager.usedModules({
179
- timing: 'load'
180
- }),
181
- cacheKey
182
- }), assets.modules(assetsManager.usedModules({
183
- timing: 'preload'
184
- }), {
185
- cacheKey
186
- })]);
187
- const {
188
- htmlAttributes,
189
- bodyAttributes,
190
- ...headProps
191
- } = htmlManager.state;
192
- const minifiedHTML = '<!DOCTYPE html>' + renderToStaticMarkup(
193
-
194
- // eslint-disable-next-line jsx-a11y/html-has-lang
195
- jsxs("html", {
196
- ...htmlAttributes,
197
- children: [/*#__PURE__*/jsxs("head", {
198
- children: [/*#__PURE__*/jsx(Head, {
199
- ...headProps
200
- }), mainAssets?.scripts.map(script => /*#__PURE__*/jsx(Script, {
201
- asset: script,
202
- baseUrl: url
203
- }, script.source)), mainAssets?.styles.map(style => /*#__PURE__*/jsx(Style, {
204
- asset: style,
205
- baseUrl: url
206
- }, style.source)), preloadAssets?.styles.map(style => /*#__PURE__*/jsx(StylePreload, {
207
- asset: style,
208
- baseUrl: url
209
- }, style.source)), preloadAssets?.scripts.map(script => /*#__PURE__*/jsx(ScriptPreload, {
210
- asset: script,
211
- baseUrl: url
212
- }, script.source))]
213
- }), /*#__PURE__*/jsx("body", {
214
- ...bodyAttributes,
215
- children: /*#__PURE__*/jsx("div", {
216
- id: "app",
217
- dangerouslySetInnerHTML: {
218
- __html: markup ?? ''
219
- }
220
- })
221
- })]
222
- }));
223
- const html = prettify ? await prettifyHTML(minifiedHTML) : minifiedHTML;
224
- return {
225
- html,
226
- http: http.state,
227
- routes: routeRecorder.state
228
- };
229
- }
230
- }
231
- async function prettifyHTML(html) {
232
- try {
233
- const {
234
- default: prettier
235
- } = await import('prettier');
236
- return prettier.format(html, {
237
- parser: 'html'
238
- });
239
- } catch (error) {
240
- return html;
241
- }
242
- }
243
- function joinPath(...parts) {
244
- let path = '/';
245
- for (const part of parts) {
246
- if (typeof part !== 'string') continue;
247
- const normalizedPart = part.startsWith('/') ? part.slice(1) : part;
248
- if (normalizedPart.length === 0) continue;
249
- if (path !== '/') path += '/';
250
- path += normalizedPart;
251
- path = removePostfixSlash(path);
252
- }
253
- return path;
254
- }
255
- function removePostfixSlash(value) {
256
- return value.endsWith('/') && value !== '/' ? value.slice(0, value.length - 1) : value;
257
- }
258
-
259
- export { renderStatic };
@@ -1,42 +0,0 @@
1
- import { extract } from '@quilted/react-server-render/server';
2
- import { HTMLManager } from '@quilted/react-html/server';
3
- import { HttpManager } from '@quilted/react-http/server';
4
- import { AssetsManager } from '@quilted/react-assets/server';
5
- import { StaticContext } from './StaticContext.esnext';
6
- import { jsx } from 'react/jsx-runtime';
7
-
8
- async function renderApp(app, {
9
- decorate,
10
- url,
11
- headers,
12
- cacheKey,
13
- ...rest
14
- } = {}) {
15
- const html = new HTMLManager();
16
- const http = new HttpManager({
17
- headers
18
- });
19
- const assets = new AssetsManager({
20
- cacheKey
21
- });
22
- const markup = await extract(app, {
23
- decorate(app) {
24
- return /*#__PURE__*/jsx(StaticContext, {
25
- html: html,
26
- http: http,
27
- url: url,
28
- assets: assets,
29
- children: decorate?.(app) ?? app
30
- });
31
- },
32
- ...rest
33
- });
34
- return {
35
- markup,
36
- http,
37
- html,
38
- assets
39
- };
40
- }
41
-
42
- export { renderApp };
@@ -1 +0,0 @@
1
- export * from '@quilted/testing';