@hubspot/cms-dev-server 0.4.18 → 0.5.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/dist/components/ModulesList.js +14 -0
- package/dist/components/PartialsList.js +13 -0
- package/dist/createDevServer.js +162 -0
- package/dist/createViteDevServer.js +51 -0
- package/dist/fsevents-72LCIACT.node +0 -0
- package/dist/index.js +2 -2
- package/dist/internalSsr.js +13 -6
- package/dist/lib/cli.js +27 -0
- package/dist/lib/debug.js +2 -0
- package/dist/lib/errors.js +22 -0
- package/dist/lib/fullOriginalUrlFromRequest.js +3 -0
- package/dist/lib/httpsRedirectingServer.js +48 -0
- package/dist/lib/moduleGraph.js +11 -0
- package/dist/lib/proxyHelpers.js +70 -0
- package/dist/lib/proxyHelpers.test.js +136 -0
- package/dist/lib/renderComponentWithCSS.js +26 -0
- package/dist/lib/renderHelpers.js +45 -0
- package/dist/lib/renderModule.js +56 -0
- package/dist/lib/renderPartial.js +15 -0
- package/dist/lib/templates.js +55 -0
- package/dist/proxyPage/constants.js +1 -0
- package/dist/proxyPage/fetchContentMetadata.js +59 -0
- package/dist/proxyPage/fetchDomains.js +29 -0
- package/dist/proxyPage/fetchPreviewInfo.js +37 -0
- package/dist/proxyPage/proxyPage.js +174 -0
- package/dist/proxyPage/renderLocalError.js +27 -0
- package/dist/proxyPage/utils.js +38 -0
- package/dist/routes/createDevServerRoutes.js +43 -0
- package/dist/routes/homeRouteHandler.js +12 -0
- package/dist/routes/linksToAllModulesRouteHandler.js +10 -0
- package/dist/routes/linksToAllPartialsRouteHandler.js +10 -0
- package/dist/routes/module.js +41 -0
- package/dist/routes/partial.js +30 -0
- package/dist/routes/proxyPageResourceRedirect.js +22 -0
- package/dist/routes/proxyPageRouteHandler.js +40 -0
- package/dist/routes/proxyPathPageResourceRedirect.js +36 -0
- package/dist/routes/proxyPathPageRouteHandler.js +43 -0
- package/dist/run.js +95 -35
- package/dist/shadowDevServer/createShadowDevServerRoutes.js +63 -0
- package/dist/shadowDevServer/startShadowDevServer.js +31 -0
- package/dist/storybook/getFieldsArgTypes.js +54 -0
- package/dist/storybook/getFieldsArgTypes.test.js +91 -0
- package/dist/storybook/getNestedFieldObjectForArgs.js +22 -0
- package/dist/storybook/getNestedFieldObjectForArgs.test.js +40 -0
- package/dist/storybook/index.js +1 -1
- package/dist/storybook/main.js +13 -6
- package/dist/storybook/moduleStory.js +61 -0
- package/dist/storybook/preview.js +9 -0
- package/dist/storybook/types.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { buildStaticEnvConfig } from '@hubspot/cms-components/config';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import url from 'url';
|
|
4
|
+
import { normalizePath } from 'vite';
|
|
5
|
+
import fullOriginalUrlFromRequest from '../lib/fullOriginalUrlFromRequest.js';
|
|
6
|
+
import renderModule from '../lib/renderModule.js';
|
|
7
|
+
import { createHtmlShellDependingOnRenderContext } from '../lib/templates.js';
|
|
8
|
+
const moduleRouteHandler = async (request, response) => {
|
|
9
|
+
const { authedPortalId, authedEnv, authedHublet, webRoot, viteServer } = request.app.locals;
|
|
10
|
+
const moduleName = request.params.moduleName;
|
|
11
|
+
const baseTemplatePath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '../html/baseTemplate.html');
|
|
12
|
+
const modulePath = normalizePath(path.resolve(webRoot, 'components/modules/', moduleName));
|
|
13
|
+
let fieldOverrides = null;
|
|
14
|
+
if (request.query.fields) {
|
|
15
|
+
try {
|
|
16
|
+
if (typeof request.query.fields === 'string') {
|
|
17
|
+
fieldOverrides = JSON.parse(request.query.fields);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.error(`Error parsing field param for module ${moduleName}: ${request.query.fields}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const environmentConfig = buildStaticEnvConfig(authedPortalId, authedHublet, authedEnv, false);
|
|
25
|
+
try {
|
|
26
|
+
const { html: renderedHtml, cssNeeded, renderContext, } = await renderModule(viteServer, modulePath, fieldOverrides, fullOriginalUrlFromRequest(request), environmentConfig);
|
|
27
|
+
const htmlShell = await createHtmlShellDependingOnRenderContext(baseTemplatePath, request.originalUrl, `Module: ${modulePath}`, renderContext, viteServer);
|
|
28
|
+
const html = htmlShell
|
|
29
|
+
.replace('<!--headend-->', cssNeeded)
|
|
30
|
+
.replace(`<!--ssr-outlet-->`, renderedHtml);
|
|
31
|
+
response.status(200).set({ 'Content-Type': 'text/html' }).end(html);
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.error(e);
|
|
35
|
+
viteServer.ssrFixStacktrace(e);
|
|
36
|
+
response
|
|
37
|
+
.status(500)
|
|
38
|
+
.end(`Error rendering module (${moduleName})\n\n${e.stack}`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export default moduleRouteHandler;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import url from 'url';
|
|
3
|
+
import { normalizePath } from 'vite';
|
|
4
|
+
import fullOriginalUrlFromRequest from '../lib/fullOriginalUrlFromRequest.js';
|
|
5
|
+
import renderPartial from '../lib/renderPartial.js';
|
|
6
|
+
import { createHtmlShellDependingOnRenderContext } from '../lib/templates.js';
|
|
7
|
+
import { buildStaticEnvConfig } from '@hubspot/cms-components/config';
|
|
8
|
+
const partialRouteHandler = async (request, response) => {
|
|
9
|
+
const { authedPortalId, authedEnv, authedHublet, webRoot, viteServer } = request.app.locals;
|
|
10
|
+
const partialName = request.params.partial;
|
|
11
|
+
const baseTemplatePath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '../html/baseTemplate.html');
|
|
12
|
+
const partialPath = normalizePath(path.resolve(webRoot, 'components/partials/', partialName));
|
|
13
|
+
const environmentConfig = buildStaticEnvConfig(authedPortalId, authedHublet, authedEnv, false);
|
|
14
|
+
try {
|
|
15
|
+
const { html: renderedHtml, cssNeeded, renderContext, } = await renderPartial(viteServer, partialPath, {}, fullOriginalUrlFromRequest(request), environmentConfig);
|
|
16
|
+
const htmlShell = await createHtmlShellDependingOnRenderContext(baseTemplatePath, request.originalUrl, `Partial: ${partialPath}`, renderContext, viteServer);
|
|
17
|
+
const html = htmlShell
|
|
18
|
+
.replace('<!--headend-->', cssNeeded)
|
|
19
|
+
.replace(`<!--ssr-outlet-->`, renderedHtml);
|
|
20
|
+
response.status(200).set({ 'Content-Type': 'text/html' }).end(html);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
console.error(e);
|
|
24
|
+
viteServer.ssrFixStacktrace(e);
|
|
25
|
+
response
|
|
26
|
+
.status(500)
|
|
27
|
+
.end(`Error rendering partial (${partialName})\n\n${e.stack}`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export default partialRouteHandler;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import request from 'request';
|
|
2
|
+
import { getSubDomainFromValidLocalDomain } from '../proxyPage/utils.js';
|
|
3
|
+
import { VALID_PROXY_DOMAIN_SUFFIXES } from '../proxyPage/constants.js';
|
|
4
|
+
// From(ish) https://git.hubteam.com/HubSpot/cloudflare-workers/blob/master/worker-lib/src/Constants.ts
|
|
5
|
+
const HCMS_PATH = '/_hcms/';
|
|
6
|
+
const HS_FS_PATH = '/hs-fs/';
|
|
7
|
+
const HUB_FS_PATH = '/hubfs/';
|
|
8
|
+
const isInternalCMSRoute = (req) => req.path.startsWith(HCMS_PATH) ||
|
|
9
|
+
req.path.startsWith(HS_FS_PATH) ||
|
|
10
|
+
req.path.startsWith(HUB_FS_PATH);
|
|
11
|
+
export default function proxyPageResourceRedirect(req, res, next) {
|
|
12
|
+
const isAHtmlRequest = req.accepts().includes('text/html');
|
|
13
|
+
if ((isAHtmlRequest && !isInternalCMSRoute(req)) ||
|
|
14
|
+
VALID_PROXY_DOMAIN_SUFFIXES.includes(req.hostname)) {
|
|
15
|
+
next();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const domainToProxy = getSubDomainFromValidLocalDomain(req.hostname);
|
|
19
|
+
const pathToProxy = req.originalUrl;
|
|
20
|
+
const urlToProxy = `https://${domainToProxy}${pathToProxy}`;
|
|
21
|
+
request(urlToProxy).pipe(res);
|
|
22
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* hs-eslint ignored failing-rules */
|
|
2
|
+
/* eslint-disable consistent-return */
|
|
3
|
+
import fetchContentMetadata from '../proxyPage/fetchContentMetadata.js';
|
|
4
|
+
import proxyPage from '../proxyPage/proxyPage.js';
|
|
5
|
+
import { getSubDomainFromValidLocalDomain, consoleWarn, } from '../proxyPage/utils.js';
|
|
6
|
+
import { VALID_PROXY_DOMAIN_SUFFIXES } from '../proxyPage/constants.js';
|
|
7
|
+
import fullOriginalUrlFromRequest from '../lib/fullOriginalUrlFromRequest.js';
|
|
8
|
+
export default async function proxyPageRouteHandler(request, response, next) {
|
|
9
|
+
const { viteServer, webRoot, projectName, projectComponentPath } = request.app.locals;
|
|
10
|
+
if (!request.accepts().includes('text/html')) {
|
|
11
|
+
return response.sendStatus(404);
|
|
12
|
+
}
|
|
13
|
+
const fullOriginalUrl = fullOriginalUrlFromRequest(request);
|
|
14
|
+
const domainToProxy = getSubDomainFromValidLocalDomain(request.hostname);
|
|
15
|
+
const urlToProxy = `http://${domainToProxy}${request.originalUrl}`;
|
|
16
|
+
if (!domainToProxy) {
|
|
17
|
+
if (VALID_PROXY_DOMAIN_SUFFIXES.includes(request.hostname)) {
|
|
18
|
+
const message = `Can't make a proxy request, you need to prepend the real page's domain to '${request.hostname}' (i.e. your.page.com.${request.hostname})`;
|
|
19
|
+
consoleWarn(message);
|
|
20
|
+
return response.status(400).send(message);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
const message = `Can't make a proxy request, the domain '${request.hostname}' is not a valid proxy-able domain`;
|
|
24
|
+
consoleWarn(message);
|
|
25
|
+
return response.status(400).send(message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (request.query.hs_preview) {
|
|
29
|
+
const message = `Can't make a proxy request, you cannot proxy URLs that include internal query params like hs_preview`;
|
|
30
|
+
consoleWarn(message);
|
|
31
|
+
return response.status(400).send(message);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const { portalId, contentId, hublet } = await fetchContentMetadata(urlToProxy);
|
|
35
|
+
await proxyPage(response, portalId, hublet, contentId, webRoot, viteServer, projectName, projectComponentPath, urlToProxy, fullOriginalUrl);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
next(e);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
const HCMS_PATH = '/_hcms/';
|
|
3
|
+
const HS_FS_PATH = '/hs-fs/';
|
|
4
|
+
const HUB_FS_PATH = '/hubfs/';
|
|
5
|
+
// From(ish) https://git.hubteam.com/HubSpot/cloudflare-workers/blob/master/worker-lib/src/Constants.ts
|
|
6
|
+
const isInternalCMSRoute = (req) => req.path.startsWith(HCMS_PATH) ||
|
|
7
|
+
req.path.startsWith(HS_FS_PATH) ||
|
|
8
|
+
req.path.startsWith(HUB_FS_PATH);
|
|
9
|
+
export default function proxyPathPageResourceRedirect(req, res, next) {
|
|
10
|
+
const isAHtmlRequest = req.accepts().includes('text/html');
|
|
11
|
+
// only proxy when referer's path is /proxy && has page query param
|
|
12
|
+
let proxyPageUrl;
|
|
13
|
+
let refererUrl;
|
|
14
|
+
try {
|
|
15
|
+
refererUrl = new URL(req.headers.referer);
|
|
16
|
+
proxyPageUrl = new URL(new URL(req.headers.referer).searchParams.get('page'));
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
next();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!refererUrl.pathname.startsWith('/proxy')) {
|
|
23
|
+
next();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// handle anchor links, unless internal CMS route
|
|
27
|
+
if (isAHtmlRequest && !isInternalCMSRoute(req)) {
|
|
28
|
+
const encodedPageUrl = encodeURIComponent(`${proxyPageUrl.origin}${req.url}`);
|
|
29
|
+
res.redirect(`/proxy?page=${encodedPageUrl}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const urlToProxy = `https://${proxyPageUrl.host}${req.url}`;
|
|
33
|
+
fetch(urlToProxy)
|
|
34
|
+
.then(response => response.body.pipe(res))
|
|
35
|
+
.catch(err => console.log(err));
|
|
36
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/* hs-eslint ignored failing-rules */
|
|
3
|
+
/* eslint-disable consistent-return */
|
|
4
|
+
import { renderOnServer } from '@hubspot/cms-js-internal';
|
|
5
|
+
import fetchContentMetadata from '../proxyPage/fetchContentMetadata.js';
|
|
6
|
+
import proxyPage from '../proxyPage/proxyPage.js';
|
|
7
|
+
import { consoleWarn } from '../proxyPage/utils.js';
|
|
8
|
+
export default async function proxyPathPageRouteHandler(request, response, next) {
|
|
9
|
+
const { viteServer, webRoot, devServerHost, devServerPort, projectName, projectComponentPath, } = request.app.locals;
|
|
10
|
+
if (!request.accepts().includes('text/html')) {
|
|
11
|
+
return response.sendStatus(404);
|
|
12
|
+
}
|
|
13
|
+
// render proxy index page if not supplied with query param
|
|
14
|
+
if (!request.query.page) {
|
|
15
|
+
const { html: responseHtml } = await renderOnServer(_jsxs("div", { children: [_jsx("h2", { children: "Local Proxy" }), _jsxs("form", { action: "/proxy", children: [_jsx("label", { htmlFor: "page", style: { display: 'block' }, children: "Page URL" }), _jsx("input", { style: { width: '300px' }, name: "page", type: "text", placeholder: "https://somecmspage.com/path" }), _jsx("button", { style: { display: 'block' }, children: "Proxy" })] })] }), null, null);
|
|
16
|
+
response.status(200).set({ 'Content-Type': 'text/html' }).end(responseHtml);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
// parse proxy page URL from query param
|
|
20
|
+
let proxyPageUrl;
|
|
21
|
+
try {
|
|
22
|
+
proxyPageUrl = new URL(request.query.page);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
const message = 'Please provide a valid page query parameter, e.g., http://localhost:3000/proxy?page=https://yourdomain.com/path';
|
|
26
|
+
consoleWarn(message);
|
|
27
|
+
response.status(400).send(message);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// validate proxy page URL query params
|
|
31
|
+
if (proxyPageUrl.searchParams.has('hs_preview')) {
|
|
32
|
+
const message = `Can't make a proxy request, you cannot proxy URLs that include internal query params like hs_preview`;
|
|
33
|
+
consoleWarn(message);
|
|
34
|
+
return response.status(400).send(message);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const { portalId, contentId, hublet } = await fetchContentMetadata(proxyPageUrl.href);
|
|
38
|
+
await proxyPage(response, portalId, hublet, contentId, webRoot, viteServer, projectName, projectComponentPath, proxyPageUrl.href, proxyPageUrl.href);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
next(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/dist/run.js
CHANGED
|
@@ -60,7 +60,7 @@ import { createRequire } from "module";
|
|
|
60
60
|
import * as jsxRuntime from "react/jsx-runtime";
|
|
61
61
|
import stream from "stream";
|
|
62
62
|
import { renderToPipeableStream, renderToStaticMarkup } from "react-dom/server";
|
|
63
|
-
import {
|
|
63
|
+
import { createServerRenderContextV2, ServerRenderContext } from "@hubspot/cms-components";
|
|
64
64
|
import { createHash } from "crypto";
|
|
65
65
|
import { existsSync, readdirSync, lstatSync } from "fs";
|
|
66
66
|
import { join } from "path/posix";
|
|
@@ -196,7 +196,7 @@ function resolveIslandPlugin(gates) {
|
|
|
196
196
|
}
|
|
197
197
|
};
|
|
198
198
|
}
|
|
199
|
-
var clientRenderIsland = "import type { RenderableIsland } from '@hubspot/cms-components';\nimport { ClientRenderContextProvider } from '@hubspot/cms-components';\nimport { Suspense } from 'react';\nimport ReactDOMClient from 'react-dom/client';\n\nexport function clientRenderIsland(\n renderableIsland: RenderableIsland,\n Component: any\n) {\n const { island, rootElement, postHydration, pageUrl, islands } =\n renderableIsland;\n let reactRoot: any;\n\n const rootOptions = {\n // Ensure useId is unique across island roots\n identifierPrefix: `island-${renderableIsland.index}-`,\n };\n\n const elementsToHydrate = (\n <Suspense fallback={`client hydrate fail/waiting for ${island.id}`}>\n <ClientRenderContextProvider\n pageUrl={pageUrl}\n islands={islands}\n onTopLevelHydrationFinish={postHydration}\n island={island}\n >\n <Component islandId={island.id} {...island.props} />\n </ClientRenderContextProvider>\n </Suspense>\n );\n\n if (island.clientOnly) {\n reactRoot = ReactDOMClient.createRoot(rootElement, rootOptions);\n reactRoot.render(elementsToHydrate);\n } else {\n reactRoot = ReactDOMClient.hydrateRoot(\n rootElement,\n elementsToHydrate,\n rootOptions\n );\n }\n return reactRoot;\n}\n";
|
|
199
|
+
var clientRenderIsland = "import type { RenderableIsland } from '@hubspot/cms-components';\nimport { ClientRenderContextProvider } from '@hubspot/cms-components';\nimport { Suspense } from 'react';\nimport ReactDOMClient from 'react-dom/client';\n\nexport function clientRenderIsland(\n renderableIsland: RenderableIsland,\n Component: any\n) {\n const { island, rootElement, postHydration, pageUrl, islands, basePath } =\n renderableIsland;\n let reactRoot: any;\n\n const rootOptions = {\n // Ensure useId is unique across island roots\n identifierPrefix: `island-${renderableIsland.index}-`,\n };\n\n const elementsToHydrate = (\n <Suspense fallback={`client hydrate fail/waiting for ${island.id}`}>\n <ClientRenderContextProvider\n pageUrl={pageUrl}\n islands={islands}\n onTopLevelHydrationFinish={postHydration}\n island={island}\n basePath={basePath}\n >\n <Component islandId={island.id} {...island.props} />\n </ClientRenderContextProvider>\n </Suspense>\n );\n\n if (island.clientOnly) {\n reactRoot = ReactDOMClient.createRoot(rootElement, rootOptions);\n reactRoot.render(elementsToHydrate);\n } else {\n reactRoot = ReactDOMClient.hydrateRoot(\n rootElement,\n elementsToHydrate,\n rootOptions\n );\n }\n return reactRoot;\n}\n";
|
|
200
200
|
var virtualClientRenderIslandModuleId = "virtual:clientRenderIsland.tsx";
|
|
201
201
|
function clientIslandEntryPlugin() {
|
|
202
202
|
return {
|
|
@@ -289,8 +289,11 @@ ${consoleLines.join("\n")}
|
|
|
289
289
|
${groupEnd}</script>
|
|
290
290
|
`;
|
|
291
291
|
};
|
|
292
|
-
async function renderOnServer(elements, pageUrl, environmentConfig, debugName, renderOptions) {
|
|
293
|
-
const renderContext =
|
|
292
|
+
async function renderOnServer(elements, pageUrl, environmentConfig, entryPointBasePath, debugName, renderOptions) {
|
|
293
|
+
const renderContext = createServerRenderContextV2(pageUrl, {
|
|
294
|
+
environmentConfig,
|
|
295
|
+
entryPointBasePath
|
|
296
|
+
});
|
|
294
297
|
let html = await renderStreamToPromise(
|
|
295
298
|
/* @__PURE__ */ jsx2(ServerRenderContext.Provider, { value: renderContext, children: elements }),
|
|
296
299
|
renderOptions
|
|
@@ -389,13 +392,16 @@ function findAllPartialsInRoot(webRoot2) {
|
|
|
389
392
|
);
|
|
390
393
|
return { partialNames, partialSubpaths };
|
|
391
394
|
}
|
|
392
|
-
function getServerGlobalsToInject(environmentConfig, hsServerPageUrl) {
|
|
395
|
+
function getServerGlobalsToInject(environmentConfig, hsServerPageUrl, hsBasePath) {
|
|
393
396
|
const globals = [
|
|
394
397
|
`window.__hsEnvConfig = ${JSON.stringify(environmentConfig.toJSON())};`
|
|
395
398
|
];
|
|
396
399
|
if (hsServerPageUrl) {
|
|
397
400
|
globals.push(`window.__hsServerPageUrl = '${hsServerPageUrl}';`);
|
|
398
401
|
}
|
|
402
|
+
if (hsBasePath) {
|
|
403
|
+
globals.push(`window.__hsBasePath = '${hsBasePath}';`);
|
|
404
|
+
}
|
|
399
405
|
return `
|
|
400
406
|
<script type="text/javascript">
|
|
401
407
|
${globals.join("\n ")}
|
|
@@ -435,16 +441,6 @@ function renderFieldsToJson(fields) {
|
|
|
435
441
|
return moduleJson;
|
|
436
442
|
}
|
|
437
443
|
|
|
438
|
-
// src/lib/moduleGraph.ts
|
|
439
|
-
var findFullPathInModuleGraph = (modulePathNoExtension, moduleGraph) => {
|
|
440
|
-
for (const extension of COMPONENT_EXTENSIONS) {
|
|
441
|
-
const pathToTry = `${modulePathNoExtension}${extension}`;
|
|
442
|
-
if (moduleGraph.fileToModulesMap.has(pathToTry)) {
|
|
443
|
-
return pathToTry;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
};
|
|
447
|
-
|
|
448
444
|
// src/lib/packagePath.ts
|
|
449
445
|
import path2 from "path";
|
|
450
446
|
import url from "url";
|
|
@@ -462,10 +458,18 @@ function packagePath(metaUrl, projectPath) {
|
|
|
462
458
|
);
|
|
463
459
|
}
|
|
464
460
|
|
|
465
|
-
// src/lib/
|
|
466
|
-
|
|
467
|
-
const
|
|
468
|
-
|
|
461
|
+
// src/lib/moduleGraph.ts
|
|
462
|
+
var findFullPathInModuleGraph = (modulePathNoExtension, moduleGraph) => {
|
|
463
|
+
for (const extension of COMPONENT_EXTENSIONS) {
|
|
464
|
+
const pathToTry = `${modulePathNoExtension}${extension}`;
|
|
465
|
+
if (moduleGraph.fileToModulesMap.has(pathToTry)) {
|
|
466
|
+
return pathToTry;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// src/lib/pathHelpers.ts
|
|
472
|
+
var getComponentPaths = (viteServer, componentPath) => {
|
|
469
473
|
let componentPathWithExtension;
|
|
470
474
|
let componentPathNoExtension;
|
|
471
475
|
if (hasComponentExtension(componentPath)) {
|
|
@@ -474,13 +478,6 @@ async function renderComponentWithCSS(viteServer, componentPath, Component, prop
|
|
|
474
478
|
} else {
|
|
475
479
|
componentPathNoExtension = componentPath;
|
|
476
480
|
}
|
|
477
|
-
const { html, renderContext } = await renderComponentOnServer(
|
|
478
|
-
Component,
|
|
479
|
-
props,
|
|
480
|
-
pageUrl,
|
|
481
|
-
environmentConfig,
|
|
482
|
-
renderOptions
|
|
483
|
-
);
|
|
484
481
|
if (!componentPathWithExtension) {
|
|
485
482
|
componentPathWithExtension = findFullPathInModuleGraph(
|
|
486
483
|
componentPathNoExtension,
|
|
@@ -492,6 +489,34 @@ async function renderComponentWithCSS(viteServer, componentPath, Component, prop
|
|
|
492
489
|
);
|
|
493
490
|
}
|
|
494
491
|
}
|
|
492
|
+
return [componentPathNoExtension, componentPathWithExtension];
|
|
493
|
+
};
|
|
494
|
+
var getEntryPointBasePath = (componentPath) => {
|
|
495
|
+
if (componentPath.includes(ENTRY_POINTS_PATH)) {
|
|
496
|
+
return `partial/${componentPath.split(ENTRY_POINTS_PATH)[1]}`;
|
|
497
|
+
}
|
|
498
|
+
if (componentPath.includes(MODULE_FOLDER_PATH)) {
|
|
499
|
+
return `module/${componentPath.split(MODULE_FOLDER_PATH)[1]}`;
|
|
500
|
+
}
|
|
501
|
+
throw new Error("Unknown entry point path type");
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// src/lib/renderComponentWithCSS.ts
|
|
505
|
+
async function renderComponentWithCSS(viteServer, componentPath, Component, props, pageUrl, entryPointBasePath, environmentConfig, renderOptions) {
|
|
506
|
+
const internalSsrPath = packagePath(import.meta.url, "internalSsr");
|
|
507
|
+
const { renderComponentOnServer, collectCssInDev } = await viteServer.ssrLoadModule(internalSsrPath);
|
|
508
|
+
const [, componentPathWithExtension] = getComponentPaths(
|
|
509
|
+
viteServer,
|
|
510
|
+
componentPath
|
|
511
|
+
);
|
|
512
|
+
const { html, renderContext } = await renderComponentOnServer(
|
|
513
|
+
Component,
|
|
514
|
+
props,
|
|
515
|
+
pageUrl,
|
|
516
|
+
environmentConfig,
|
|
517
|
+
entryPointBasePath,
|
|
518
|
+
renderOptions
|
|
519
|
+
);
|
|
495
520
|
const cssNeeded = collectCssInDev(componentPathWithExtension, viteServer);
|
|
496
521
|
return { html, cssNeeded, renderContext };
|
|
497
522
|
}
|
|
@@ -539,7 +564,7 @@ var logPartialInfo = (partialPath, props) => {
|
|
|
539
564
|
};
|
|
540
565
|
|
|
541
566
|
// src/lib/renderPartial.ts
|
|
542
|
-
async function renderPartial(viteServer, partialPath, props, pageUrl, environmentConfig, renderOptions) {
|
|
567
|
+
async function renderPartial(viteServer, partialPath, props, pageUrl, basePath, environmentConfig, renderOptions) {
|
|
543
568
|
logPartialInfo(partialPath, props);
|
|
544
569
|
let serverSideProps;
|
|
545
570
|
const componentModule = await viteServer.ssrLoadModule(partialPath);
|
|
@@ -571,6 +596,7 @@ async function renderPartial(viteServer, partialPath, props, pageUrl, environmen
|
|
|
571
596
|
Component,
|
|
572
597
|
{ ...props, ...serverSideProps },
|
|
573
598
|
pageUrl,
|
|
599
|
+
basePath,
|
|
574
600
|
environmentConfig,
|
|
575
601
|
renderOptions
|
|
576
602
|
);
|
|
@@ -632,7 +658,8 @@ var createHtmlShellDependingOnRenderContext = async (baseTemplatePath, url3, tit
|
|
|
632
658
|
if (renderContext.islands.length) {
|
|
633
659
|
const globalScriptBlock = getServerGlobalsToInject(
|
|
634
660
|
renderContext.environmentConfig,
|
|
635
|
-
renderContext.pageUrl
|
|
661
|
+
renderContext.pageUrl,
|
|
662
|
+
renderContext.entryPointBasePath
|
|
636
663
|
);
|
|
637
664
|
htmlTemplate = await createIslandHtmlTemplate(
|
|
638
665
|
baseTemplatePath,
|
|
@@ -711,6 +738,7 @@ var partialRouteHandler = async (request2, response) => {
|
|
|
711
738
|
partialName
|
|
712
739
|
].join("/")
|
|
713
740
|
);
|
|
741
|
+
const basePath = getEntryPointBasePath(partialPath);
|
|
714
742
|
const environmentConfig = buildStaticEnvConfig(
|
|
715
743
|
authedPortalId,
|
|
716
744
|
authedHublet,
|
|
@@ -728,6 +756,7 @@ var partialRouteHandler = async (request2, response) => {
|
|
|
728
756
|
partialPath,
|
|
729
757
|
{},
|
|
730
758
|
fullOriginalUrlFromRequest(request2),
|
|
759
|
+
basePath,
|
|
731
760
|
environmentConfig,
|
|
732
761
|
{
|
|
733
762
|
identifierPrefix: getEntryPointHash(entrypointPath)
|
|
@@ -782,7 +811,8 @@ var linksToAllPartialRouteHandler = async (request2, response) => {
|
|
|
782
811
|
const { html: responseHtml } = await renderOnServer(
|
|
783
812
|
/* @__PURE__ */ jsx4(PartialsList, { webRoot: webRoot2, partialNames }),
|
|
784
813
|
null,
|
|
785
|
-
null
|
|
814
|
+
null,
|
|
815
|
+
""
|
|
786
816
|
);
|
|
787
817
|
response.status(200).set({ "Content-Type": "text/html" }).end(responseHtml);
|
|
788
818
|
};
|
|
@@ -818,7 +848,8 @@ var linksToAllModuleRouteHandler = async (request2, response) => {
|
|
|
818
848
|
const { html: responseHtml } = await renderOnServer(
|
|
819
849
|
/* @__PURE__ */ jsx6(ModulesList_default, { webRoot: webRoot2, moduleNames }),
|
|
820
850
|
null,
|
|
821
|
-
null
|
|
851
|
+
null,
|
|
852
|
+
""
|
|
822
853
|
);
|
|
823
854
|
response.status(200).set({ "Content-Type": "text/html" }).end(responseHtml);
|
|
824
855
|
};
|
|
@@ -844,7 +875,7 @@ var lookForModuleInFolder = async (modulePath, viteServer) => {
|
|
|
844
875
|
}
|
|
845
876
|
return [componentModule, insideFolderPath];
|
|
846
877
|
};
|
|
847
|
-
async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}, pageUrl, environmentConfig, renderOptions) {
|
|
878
|
+
async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}, pageUrl, basePath, environmentConfig, renderOptions) {
|
|
848
879
|
console.log("Rendering module", requestedModulePath);
|
|
849
880
|
const ungatedForServerSideProps = isUngatedFor(
|
|
850
881
|
environmentConfig.getGates(),
|
|
@@ -897,6 +928,7 @@ async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}
|
|
|
897
928
|
Component,
|
|
898
929
|
{ ...props, ...serverSideProps },
|
|
899
930
|
pageUrl,
|
|
931
|
+
basePath,
|
|
900
932
|
environmentConfig,
|
|
901
933
|
renderOptions
|
|
902
934
|
);
|
|
@@ -951,6 +983,7 @@ var moduleRouteHandler = async (request2, response) => {
|
|
|
951
983
|
);
|
|
952
984
|
}
|
|
953
985
|
}
|
|
986
|
+
const basePath = getEntryPointBasePath(modulePath);
|
|
954
987
|
const environmentConfig = buildStaticEnvConfig2(
|
|
955
988
|
authedPortalId,
|
|
956
989
|
authedHublet,
|
|
@@ -968,6 +1001,7 @@ var moduleRouteHandler = async (request2, response) => {
|
|
|
968
1001
|
modulePath,
|
|
969
1002
|
fieldOverrides,
|
|
970
1003
|
fullOriginalUrlFromRequest(request2),
|
|
1004
|
+
basePath,
|
|
971
1005
|
environmentConfig,
|
|
972
1006
|
{
|
|
973
1007
|
identifierPrefix: getEntryPointHash(entrypointPath)
|
|
@@ -1272,6 +1306,16 @@ import {
|
|
|
1272
1306
|
import {
|
|
1273
1307
|
mergeServerRequestData_internal
|
|
1274
1308
|
} from "@hubspot/cms-components";
|
|
1309
|
+
var getEntryPointBasePathFromProxyRequest = (request2) => {
|
|
1310
|
+
if (!request2) {
|
|
1311
|
+
return "";
|
|
1312
|
+
}
|
|
1313
|
+
const { path: requestPath, pathParamDict } = request2;
|
|
1314
|
+
if (!pathParamDict || !pathParamDict.dynamic_slug) {
|
|
1315
|
+
return requestPath;
|
|
1316
|
+
}
|
|
1317
|
+
return requestPath.replace(pathParamDict.dynamic_slug, "");
|
|
1318
|
+
};
|
|
1275
1319
|
var makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId, projectName, jsRenderingGates) => {
|
|
1276
1320
|
const url3 = new URL(urlToProxy);
|
|
1277
1321
|
console.log(`Proxying request to ${url3.href}`, {
|
|
@@ -1313,7 +1357,7 @@ var makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId, pro
|
|
|
1313
1357
|
}
|
|
1314
1358
|
});
|
|
1315
1359
|
};
|
|
1316
|
-
var renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder, webRoot2, viteServer, pageUrl, environmentConfig, hsProjectJsonProjectName, webRootProjectComponent) => {
|
|
1360
|
+
var renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder, webRoot2, viteServer, pageUrl, basePath, environmentConfig, hsProjectJsonProjectName, webRootProjectComponent) => {
|
|
1317
1361
|
const proxySchemaVersion = isUngatedFor(
|
|
1318
1362
|
environmentConfig.getGates(),
|
|
1319
1363
|
"CMS:JSRenderer:LocalProxySchemaVersion1"
|
|
@@ -1354,6 +1398,7 @@ var renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder,
|
|
|
1354
1398
|
resolvedPath,
|
|
1355
1399
|
localRenderPlaceholderProps,
|
|
1356
1400
|
pageUrl,
|
|
1401
|
+
basePath,
|
|
1357
1402
|
environmentConfig,
|
|
1358
1403
|
{
|
|
1359
1404
|
identifierPrefix: getEntryPointHashWithIndexSuffix(
|
|
@@ -1368,6 +1413,7 @@ var renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder,
|
|
|
1368
1413
|
resolvedPath,
|
|
1369
1414
|
localRenderPlaceholderProps,
|
|
1370
1415
|
pageUrl,
|
|
1416
|
+
basePath,
|
|
1371
1417
|
environmentConfig,
|
|
1372
1418
|
{
|
|
1373
1419
|
identifierPrefix: getEntryPointHashWithIndexSuffix(
|
|
@@ -1427,6 +1473,7 @@ async function proxySpecificPage(response, portalId, hublet, contentId, webRoot2
|
|
|
1427
1473
|
let numJsRenderFailures = 0;
|
|
1428
1474
|
let islands = [];
|
|
1429
1475
|
let mergedPerRequestState = /* @__PURE__ */ new Map();
|
|
1476
|
+
let basePath;
|
|
1430
1477
|
const renderPromisesByChunkIndex = new Array(
|
|
1431
1478
|
numPlaceholders
|
|
1432
1479
|
);
|
|
@@ -1439,12 +1486,21 @@ async function proxySpecificPage(response, portalId, hublet, contentId, webRoot2
|
|
|
1439
1486
|
offsetSoFar,
|
|
1440
1487
|
localRenderPlaceholder.startOffset
|
|
1441
1488
|
);
|
|
1489
|
+
if (basePath === void 0 && isUngatedFor(
|
|
1490
|
+
environmentConfig.getGates(),
|
|
1491
|
+
"CMS:JSRenderer:LocalProxySchemaVersion1"
|
|
1492
|
+
)) {
|
|
1493
|
+
basePath = getEntryPointBasePathFromProxyRequest(
|
|
1494
|
+
localRenderPlaceholder.json.request
|
|
1495
|
+
);
|
|
1496
|
+
}
|
|
1442
1497
|
const renderPromise = renderComponentForPlaceholder(
|
|
1443
1498
|
indexForThisRender,
|
|
1444
1499
|
localRenderPlaceholder,
|
|
1445
1500
|
webRoot2,
|
|
1446
1501
|
viteServer,
|
|
1447
1502
|
fullOriginalUrl,
|
|
1503
|
+
basePath,
|
|
1448
1504
|
environmentConfig,
|
|
1449
1505
|
projectName,
|
|
1450
1506
|
projectComponentPath
|
|
@@ -1488,7 +1544,7 @@ ${html}`;
|
|
|
1488
1544
|
response.write(foobar);
|
|
1489
1545
|
}
|
|
1490
1546
|
response.write(
|
|
1491
|
-
getServerGlobalsToInject(environmentConfig, fullOriginalUrl)
|
|
1547
|
+
getServerGlobalsToInject(environmentConfig, fullOriginalUrl, basePath)
|
|
1492
1548
|
);
|
|
1493
1549
|
if (islands.length > 0) {
|
|
1494
1550
|
const sortedIslandRoots = sortIslandRootsByPriority(islands);
|
|
@@ -1603,7 +1659,8 @@ var homeRouteHandler = async (request2, response) => {
|
|
|
1603
1659
|
/* @__PURE__ */ jsx7(ModulesList_default, { webRoot: webRoot2, moduleNames })
|
|
1604
1660
|
] }),
|
|
1605
1661
|
null,
|
|
1606
|
-
null
|
|
1662
|
+
null,
|
|
1663
|
+
""
|
|
1607
1664
|
);
|
|
1608
1665
|
response.status(200).set({ "Content-Type": "text/html" }).end(responseHtml);
|
|
1609
1666
|
};
|
|
@@ -1645,7 +1702,8 @@ async function proxyPathPageRouteHandler(request2, response, next) {
|
|
|
1645
1702
|
] })
|
|
1646
1703
|
] }),
|
|
1647
1704
|
null,
|
|
1648
|
-
null
|
|
1705
|
+
null,
|
|
1706
|
+
""
|
|
1649
1707
|
);
|
|
1650
1708
|
response.status(200).set({ "Content-Type": "text/html" }).end(responseHtml);
|
|
1651
1709
|
return;
|
|
@@ -1746,8 +1804,10 @@ var createDevServerRoutes = async (viteServer, storybookPort) => {
|
|
|
1746
1804
|
devServerApp.get("/proxy", proxyPathPageRouteHandler);
|
|
1747
1805
|
devServerApp.get("/partials/", linksToAllPartialsRouteHandler_default);
|
|
1748
1806
|
devServerApp.get("/partial/:partial", partial_default);
|
|
1807
|
+
devServerApp.get("/partial/:partial/*", partial_default);
|
|
1749
1808
|
devServerApp.get("/modules/", linksToAllModulesRouteHandler_default);
|
|
1750
1809
|
devServerApp.get("/module/:moduleName", module_default);
|
|
1810
|
+
devServerApp.get("/module/:moduleName/*", module_default);
|
|
1751
1811
|
devServerApp.get("/", homeRouteHandler_default);
|
|
1752
1812
|
devServerApp.get("/*", proxyPathPageResourceRedirect);
|
|
1753
1813
|
devServerApp.get("/*", proxyPageResourceRedirect);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* hs-eslint ignored failing-rules */
|
|
2
|
+
/* eslint-disable require-await */
|
|
3
|
+
import { Router } from 'express';
|
|
4
|
+
import { getAccountId } from '@hubspot/cli-lib/lib/config.js';
|
|
5
|
+
import cors from 'cors';
|
|
6
|
+
import { findLocalRenderPlaceholders } from '../lib/proxyHelpers.js';
|
|
7
|
+
import { makeProxyPreviewRequest } from '../proxyPage/proxyPage.js';
|
|
8
|
+
import fetchPreviewInfo from '../proxyPage/fetchPreviewInfo.js';
|
|
9
|
+
function getShadowDevServerCheckHandler(devServerPort) {
|
|
10
|
+
return async function shadowDevServerCheckHandler(request, response) {
|
|
11
|
+
const { query } = request;
|
|
12
|
+
if (query) {
|
|
13
|
+
const { hostName, pathName, protocol, contentId, portalId: portalIdString, } = query;
|
|
14
|
+
const pageUrl = `${protocol}//${hostName}${pathName}`;
|
|
15
|
+
const portalId = parseInt(portalIdString, 10);
|
|
16
|
+
let accountId;
|
|
17
|
+
let hasJSBuildingBlocks = false;
|
|
18
|
+
let localProxyUrl;
|
|
19
|
+
try {
|
|
20
|
+
accountId = getAccountId(portalId);
|
|
21
|
+
if (accountId === null) {
|
|
22
|
+
throw new Error();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
response
|
|
27
|
+
.status(500)
|
|
28
|
+
.end(`Unable to find account for hubid = ${portalId}. Please check your hubspot.config.yaml`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const previewInfo = await fetchPreviewInfo(accountId, portalId, contentId);
|
|
33
|
+
const { previewKey } = previewInfo;
|
|
34
|
+
const pageHtml = await makeProxyPreviewRequest(accountId, pageUrl, previewKey, contentId);
|
|
35
|
+
const localRenderPlaceholders = findLocalRenderPlaceholders(pageHtml);
|
|
36
|
+
if (localRenderPlaceholders.length) {
|
|
37
|
+
hasJSBuildingBlocks = true;
|
|
38
|
+
localProxyUrl = `http://${hostName}.hslocal.net:${devServerPort}${pathName}`;
|
|
39
|
+
}
|
|
40
|
+
response
|
|
41
|
+
.status(200)
|
|
42
|
+
.set({ 'Content-Type': 'application/json' })
|
|
43
|
+
.json({ hasJSBuildingBlocks, localProxyUrl });
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
response
|
|
47
|
+
.status(500)
|
|
48
|
+
.end(`Failed proxy render of page id = ${contentId} hub id = ${portalId}\n\n${error.stack}`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
response
|
|
54
|
+
.status(500)
|
|
55
|
+
.end(`Missing pageUrl param, cannot check for proxied page without a pageUrl`);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export default async function createShadowDevServerRoutes(devServerPort) {
|
|
60
|
+
const shadowDevServerRoutes = Router();
|
|
61
|
+
shadowDevServerRoutes.get('/check-if-local-dev-server', cors(), getShadowDevServerCheckHandler(devServerPort));
|
|
62
|
+
return shadowDevServerRoutes;
|
|
63
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import createShadowDevServerRoutes from './createShadowDevServerRoutes.js';
|
|
4
|
+
const testNetServer = net.createServer();
|
|
5
|
+
export default async function startShadowDevServer(devServerPort, shadowPort) {
|
|
6
|
+
const portIsTaken = await new Promise((res, rej) => {
|
|
7
|
+
console.log(`Starting proxy link server on port ${shadowPort}`);
|
|
8
|
+
testNetServer.once('error', err => {
|
|
9
|
+
if (err['code'] === 'EADDRINUSE') {
|
|
10
|
+
console.error(`Port ${shadowPort} is in use. HubSpot is unable to automatically create proxy links in the Sprocket Menu`);
|
|
11
|
+
res(true);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
rej(err);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
testNetServer.once('listening', () => {
|
|
18
|
+
testNetServer.close();
|
|
19
|
+
});
|
|
20
|
+
testNetServer.once('close', () => {
|
|
21
|
+
res(false);
|
|
22
|
+
});
|
|
23
|
+
testNetServer.listen(shadowPort);
|
|
24
|
+
});
|
|
25
|
+
if (portIsTaken) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const shadowDevServer = express();
|
|
29
|
+
shadowDevServer.listen(shadowPort);
|
|
30
|
+
shadowDevServer.use('/', await createShadowDevServerRoutes(devServerPort));
|
|
31
|
+
}
|