tinacms / tinacms

A fully open-source headless CMS that supports Markdown and Visual Editing
https://tina.io
Apache License 2.0
11.18k stars 562 forks source link

Self-Hosted contentApiUrlOverride Absolute URL #4435

Open tchanxx opened 2 months ago

tchanxx commented 2 months ago

Describe the issue that you're seeing. Any Loom videos or screenshots usually help a lot!

Hi, I am trying to self-host the Tina Node backend.

I noticed that when I set contentApiUrlOverride to an absolute URL like contentApiUrlOverride: "http://localhost:3000/api/tina/gql" everything works as expected locally.

However, when I set contentApiUrlOverride to a relative url path like /api/tina/gql like the docs say, I get:

 ✓ Compiled /[slug] in 1064ms (741 modules)
 ⨯ Internal error: TypeError: Failed to parse URL from /api/tina/gql
    at new Request (node:internal/deps/undici/undici:7134:19)
    at R (/Users/yname/Projects/tina-self-hosted-demo/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:340580)
    at doOriginalFetch (./node_modules/next/dist/server/lib/patch-fetch.js:380:24)
    at eval (./node_modules/next/dist/server/lib/patch-fetch.js:508:20)
Cause: TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:611:13)
    at new Request (node:internal/deps/undici/undici:7132:25)
    at R (/Users/myname/Projects/tina-self-hosted-demo/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:340580)
    at doOriginalFetch (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/patch-fetch.js:380:24)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/patch-fetch.js:508:20) {
  input: '/api/tina/gql',
  code: 'ERR_INVALID_URL'
}

I would use the absolute URL, but this doesn't work for production purposes since Vercel won't give me the absolute URL while building.

Any ideas?

Reproduction

N/A

Steps to reproduce

No response

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 962.20 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.19 - ~/Projects/tina-self-hosted-demo/node_modules/.bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.15.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 121.0.6167.184
    Safari: 17.0
  npmPackages:
    @tinacms/cli: ^1.5.34 => 1.5.34 
    @tinacms/datalayer: ^1.2.29 => 1.2.29 
    tinacms: ^1.5.28 => 1.5.28

Validations

kldavis4 commented 2 months ago

the examples in the documentation have relative urls, so /api/tina/gql should work. It looks like you might be on older version of @tinacms/cli and @tinacms/datalayer, though, so try upgrading those and see if that resolves the issue.

tchanxx commented 2 months ago

Thanks for the reply @kldavis4 . I updated all of my packages, and I still get the same error.

I narrowed it down to this generated code:

// TinaSDK generated code
import { createClient, TinaClient } from "tinacms/dist/client";

const generateRequester = (
  client: TinaClient,
  options?: { branch?: string }
) => {
  const requester: (
    doc: any,
    vars?: any,
    options?: { branch?: string },
    client
  ) => Promise<any> = async (doc, vars, options) => {
    let url = client.apiUrl
    if (options?.branch) {
      const index = client.apiUrl.lastIndexOf('/')
      url = client.apiUrl.substring(0, index + 1) + options.branch
    }
    const data = await client.request({
      query: doc,
      variables: vars,
      url,
    })

    return { data: data?.data, errors: data?.errors, query: doc, variables: vars || {} }
  }

  return requester
}

It is failing at:

 const data = await client.request({
      query: doc,
      variables: vars,
      url,
    })

Anyway, oddly enough, I was able to fix this problem by avoiding all of the client and replacing them with databaseClient