Git Product home page Git Product logo

Comments (8)

sindresorhus avatar sindresorhus commented on September 28, 2024 2

Proposal:

If document.baseURI doesn't exist and the input is a relative URL:

  • ...if the prefixUrl option is not set, we throw an error recommending the user to set that option.
  • ...if the prefixUrl option is set, we just don't use document.baseURI.

Thoughts?

from ky.

RemyLivewall avatar RemyLivewall commented on September 28, 2024

After further investigation it seems to come from the following line of code:

ky/index.js

Line 138 in 5dbd4a2

const url = new _globalThis.URL(this._options.prefixUrl + this._input, document.baseURI);

This was added in the 0.5.1 with commit d6f8176 which fixed issue #59. As document is not available in React-Native I am not sure if this issue is in the scope for this library and what a clean fix would be if it is in the scope.

from ky.

felixjung avatar felixjung commented on September 28, 2024

This is also a problem when trying to use ky in a SSR React app. I'd love to use ky, as it's much easier to handle than direct fetch or alternatives.

Is support for SSR something you'd still consider part of this library? The _globalThis polyfill in the main index.js seems to suggest that.

from ky.

sindresorhus avatar sindresorhus commented on September 28, 2024

Is support for SSR something you'd still consider part of this library?

Yes

from ky.

sindresorhus avatar sindresorhus commented on September 28, 2024

We just need to decide how Ky should handle document.baseURI not being available.

from ky.

felixjung avatar felixjung commented on September 28, 2024

Cool! In the meantime, I solved the situation in my NextJS app by adding things like Headers and document to the global object server side. I used node-fetch to get working implementations of these.

import fetch, { Headers } from 'node-fetch'

const BASE_URL = 'http://some.url'

if (typeof window === 'undefined') {
  global.document = {
    baseURI: BASE_URL,
  }
  global.fetch = fetch
  global.Headers = Headers
}

Ky's _globalThis polyfill automatically picks up global and things work as expected.

@sindresorhus, are you already tracking this topic in a different issue? Do you have an intuition for how you'd want to see this handled?

from ky.

sholladay avatar sholladay commented on September 28, 2024

Presumably these environments have some way to get the current URL, right? For example, an equivalent of location.href.

That would be the appropriate fallback when no DOM is present.

from ky.

felixjung avatar felixjung commented on September 28, 2024

@sholladay, well when you are running in plain node, then I would think, no. Most common scenario that comes to mind, and which I ran into, is a React page being rendered inside of an express app. I don't see how ky could be made aware of the original HTTP request causing the page render, without requiring quite some developer effort.

from ky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.