one-aalam / astro-ink

Crisp, minimal, personal blog theme for Astro
https://astro-ink.vercel.app
MIT License
556 stars 93 forks source link

Failed to parse URL from undefined/api/blog/views/[Post name] #45

Closed mikwee closed 1 year ago

mikwee commented 1 year ago

When trying to read one of my posts in dev mode, I get this error:

 error   Failed to parse URL from undefined/api/blog/views/[Post name]
  File:
    /home/mikwee/קוד/personal-site-astro/src/pages/blog/[slug].astro:26:16
  Code:
    25 |         Link,
    > 26 |         TweetEmbed,
         |                ^
      27 |         YTVideoEmbed,
      28 |         Tabs
      29 |     }}/>
  Stacktrace:
TypeError: Failed to parse URL from undefined/api/blog/views/[Post name]
    at Object.fetch (node:internal/deps/undici/undici:11600:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (/src/pages/blog/[slug].astro:26:16)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/runtime/server/render/page.js:95:30)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/core/render/core.js:96:20)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/core/render/dev/index.js:109:10)
    at async handleRoute (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/route.js:152:20)
    at async run (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/request.js:46:14)
    at async runWithErrorHandling (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/controller.js:65:5)
    at async handleRequest (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/request.js:40:3)

  Cause:
TypeError: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:778:13)
    at new Request (node:internal/deps/undici/undici:7150:25)
    at fetch2 (node:internal/deps/undici/undici:10737:25)
    at Object.fetch (node:internal/deps/undici/undici:11598:18)
    at fetch (node:internal/process/pre_execution:274:25)
    at eval (/src/pages/blog/[slug].astro:26:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/runtime/server/render/page.js:95:30)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/core/render/core.js:96:20)
    at async renderPage (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/core/render/dev/index.js:109:10)
    at async handleRoute (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/route.js:152:20)
    at async run (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/request.js:46:14)
    at async runWithErrorHandling (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/controller.js:65:5)
    at async handleRequest (file:///home/mikwee/%D7%A7%D7%95%D7%93/personal-site-astro/node_modules/astro/dist/vite-plugin-astro-server/request.js:40:3)

Why could this be happening? Feel free to ask for info. Thanks.

LemosLucas commented 1 year ago

Hello, I just stumbled upon this same issue. I noticed the api calls depend on the SITE_URI environment variable being set to your domain correctly. For local development, the steps below should allow you to access the posts

mv .env.example .env

Then, set SITE_URI var.

SITE_URI=http://localhost:3000

I know it's not a good final solution, but gets the job done

one-aalam commented 1 year ago

Thank you for the help @LemosLucas 🙏 Greatly appreciated!

@mikwee SITE_URI as shared by Lucas is the right way to get rid of the error. The post detail pages use the value to form the endpoint that needs to be hit to increment the read count. The SITE_URI needs to be changed to the actual URL when your site is up/deployed through environment settings.

This is documented under "View counter feat. Redis/Upstash Redis" in the README. Let me know if this could've been easier with some improvement in the README.

one-aalam commented 1 year ago

@mikwee Marking this as closed, as what I shared above is the recommended solution.