mooxl / astroad

A pre-configured setup for easy website development with Astro and Payload CMS using Docker.
MIT License
92 stars 8 forks source link

Initial run of `yarn dev` triggers errors on a fresh install on Win 11 #158

Open fwextensions opened 9 months ago

fwextensions commented 9 months ago

This is a really handy way of getting started with Payload and Astro, which otherwise requires a bunch of moving parts. But both times I've first run yarn dev after downloading the repo and then hitting localhost, the process crashes. Seems like just killing it and running it again works, so there's maybe some timing issue.

I've installed with npx degit mooxl/astroad my-test, which should just download a clean copy of the repo.

Then I go into my-test and run yarn dev. After all the docker logs, the containers start, and the log messages seem to stop, like everything is set up. Among the mongo logs, I see the logs for Astro, like:

astroad-astro    |   🚀  astro  v2.10.12 started in 714ms
astroad-astro    |
astroad-astro    |   ┃ Local    http://localhost:3000/
astroad-astro    |   ┃ Network  http://172.22.0.3:3000/

So I hit localhost:3000, but that shows an error in the logs:

astroad-astro    |  error   Unexpected token '<', "<!DOCTYPE "... is not valid JSON
astroad-astro    |   File:
astroad-astro    |     /base/src/utils/payload.ts:8:4
astroad-astro    |   Code:
astroad-astro    |     7 | export const getPosts = async () =>
astroad-astro    |     > 8 |   (await (await fetch(`${url}/api/posts`)).json()).docs as Post[];
astroad-astro    |          |    ^
astroad-astro    |       10 | export const getPost = async (id: string) =>
astroad-astro    |       11 |   (await (await fetch(`${url}/api/posts/${id}`)).json()) as Post;
astroad-astro    |   Stacktrace:
astroad-astro    | SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
astroad-astro    |     at JSON.parse (<anonymous>)
astroad-astro    |     at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
astroad-astro    |     at successSteps (node:internal/deps/undici/undici:5300:27)
astroad-astro    |     at fullyReadBody (node:internal/deps/undici/undici:1447:9)
astroad-astro    |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
astroad-astro    |     at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
astroad-astro    |     at async Module.getPosts (/base/src/utils/payload.ts:8:4)
astroad-astro    |     at async eval (/base/src/pages/index.astro:5:15)
astroad-astro    |     at async callComponentAsTemplateResultOrResponse (file:///base/node_modules/astro/dist/runtime/server/render/astro/render.js:85:25)
astroad-astro    |     at async renderToReadableStream (file:///base/node_modules/astro/dist/runtime/server/render/astro/render.js:35:26)
astroad-astro    |

After that, I see a message about MongoDB being ready, so maybe it was still starting when I hit the page?

astroad-mongo    |
astroad-mongo    | MongoDB init process complete; ready for start up.
astroad-mongo    |

And a little below that there's a crash from payload:

astroad-payload  | [22:04:22] ERROR (payload): Error: cannot connect to MongoDB. Details: connect ECONNREFUSED 172.23.0.2:27017
astroad-payload  | [nodemon] app crashed - waiting for file changes before starting...

But if I then just hit ctrl-C and run yarn dev again, everything works fine. I'm guessing that maybe mongo wasn't fully set up when Payload was trying to connect to it on first install, and on the second launch it was. But I'm not sure how to debug that.

The above logs were from my second attempt. The first time I also got an Astro error, but a different one. The effect was the same, though.

astroad-astro    |  error   fetch failed
astroad-astro    |   File:
astroad-astro    |     /base/src/utils/payload.ts:8:11
astroad-astro    |   Code:
astroad-astro    |     7 | export const getPosts = async () =>
astroad-astro    |     > 8 |   (await (await fetch(`${url}/api/posts`)).json()).docs as Post[];
astroad-astro    |          |           ^
astroad-astro    |       10 | export const getPost = async (id: string) =>
astroad-astro    |       11 |   (await (await fetch(`${url}/api/posts/${id}`)).json()) as Post;
astroad-astro    |   Stacktrace:
astroad-astro    | TypeError: fetch failed
astroad-astro    |     at node:internal/deps/undici/undici:12344:11
astroad-astro    |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
astroad-astro    |     at async Module.getPosts (/base/src/utils/payload.ts:8:11)
astroad-astro    |     at async eval (/base/src/pages/index.astro:5:15)
astroad-astro    |     at async callComponentAsTemplateResultOrResponse (file:///base/node_modules/astro/dist/runtime/server/render/astro/render.js:85:25)
astroad-astro    |     at async renderToReadableStream (file:///base/node_modules/astro/dist/runtime/server/render/astro/render.js:35:26)
astroad-astro    |     at async renderPage (file:///base/node_modules/astro/dist/runtime/server/render/page.js:31:12)
astroad-astro    |     at async renderPage (file:///base/node_modules/astro/dist/core/render/core.js:43:18)
astroad-astro    |     at async tryRenderRoute (file:///base/node_modules/astro/dist/core/render/core.js:82:16)
astroad-astro    |     at async handleRoute (file:///base/node_modules/astro/dist/vite-plugin-astro-server/route.js:154:18)
astroad-astro    |
astroad-astro    |   Cause:
astroad-astro    | Error: connect ECONNREFUSED 172.21.0.2:3001
astroad-astro    |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)
astroad-astro    |
astroad-astro    | 07:17:45 AM [serve] 404                             /favicon.ico

Full logs: astroad logs.txt