Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content as data client breaks when prerendering with a custom dev server port #1334

Open
thescientist13 opened this issue Dec 1, 2024 · 0 comments
Labels
bug Something isn't working CLI Content as Data
Milestone

Comments

@thescientist13
Copy link
Member

Type of Change

Bug

Summary

If you change the dev server port and run a build with prerender: true, e.g.

// greenwood.config.js
export default {
  prerender: true,
  devServer: {
    port: 8181
  }
}

You will get an error running greenwood build

TypeError [Error]: fetch failed
    at node:internal/deps/undici/undici:12618:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getContentAsData (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/@greenwood/cli/src/data/client.js:9:7)
    at async getContentByCollection (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/@greenwood/cli/src/data/client.js:18:10)
    at async Header.connectedCallback (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/src/components/header/header.js:12:23)
    at async initializeCustomElement (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/wc-compiler/src/wcc.js:233:5)
    at async renderComponentRoots (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/wc-compiler/src/wcc.js:54:33)
    at async renderComponentRoots (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/wc-compiler/src/wcc.js:78:7)
    at async renderComponentRoots (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/wc-compiler/src/wcc.js:78:7)
    at async renderFromHTML (file:///Users/owenbuckley/Workspace/project-evergreen/www.greenwoodjs.dev/node_modules/wc-compiler/src/wcc.js:278:21) {
  cause: Error: connect ECONNREFUSED ::1:1984
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
    errno: -61,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 1984
  }
}

Details

This is because In the current implementation of our data client, we hardcode a fallback port in the client for communicating with the content (dev) server

const CONTENT_STATE = globalThis.__CONTENT_AS_DATA_STATE__ ?? false; // eslint-disable-line no-underscore-dangle
const PORT = globalThis?.__CONTENT_SERVER__?.PORT ?? 1984; // eslint-disable-line no-underscore-dangle
const BASE_PATH = globalThis?.__GWD_BASE_PATH__ ?? ''; // eslint-disable-line no-underscore-dangle

And since this is client side running code, it assumes that there will be a <script> tag embedded on the page with the value window.__CONTENT_SERVER__.PORT
https://github.com/ProjectEvergreen/greenwood/blob/v0.30.2/packages/cli/src/plugins/resource/plugin-active-content.js#L79

newBody = newBody.replace('<head>', `
  <head>
    <script id="content-server">
      globalThis.__CONTENT_SERVER__ = globalThis.__CONTENT_SERVER__
        ? globalThis.__CONTENT_SERVER__
        : {
            PORT: ${devServer.port}
          }
    </script>
`);

So seems we have to figure out a way to inject this into Node's globalThis scope as well (or find another solution 😅 )

@thescientist13 thescientist13 added bug Something isn't working CLI Content as Data labels Dec 1, 2024
@thescientist13 thescientist13 added this to the 1.0 milestone Dec 1, 2024
@thescientist13 thescientist13 changed the title content as data breaks when prerendering with a custom dev server port content as data client breaks when prerendering with a custom dev server port Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLI Content as Data
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant