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

[Feature?]: patch fetch on the server to allow calling api endpoints #1746

Open
2 tasks done
huseeiin opened this issue Jan 31, 2025 · 2 comments
Open
2 tasks done

[Feature?]: patch fetch on the server to allow calling api endpoints #1746

huseeiin opened this issue Jan 31, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@huseeiin
Copy link
Contributor

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

this doesn't work on the server currently:

import { createAsync, query } from "@solidjs/router";
import { Suspense } from "solid-js";

const getData = query(async () => {
  "use server";

  const res = await fetch("/api");

  return res.json();
}, "data");

export default function Home() {
  const data = createAsync(() => getData());

  return (
    <Suspense fallback={<p>Loading...</p>}>{JSON.stringify(data())}</Suspense>
  );
}

Examples 🌈

i'm getting this error: Failed to parse URL from /api

Motivation 🔦

this is implemented in sveltekit among other frameworks

@huseeiin huseeiin added the enhancement New feature or request label Jan 31, 2025
@atilafassina
Copy link
Member

I believe accepting relative paths aren't a good enough reason to patch prototype or introducing a compile-time change.

const fetchData = query(async () => {
  "use server";
  const r = await fetch("http://localhost:3001/api", { method: "GET" });
  const re = await r.json();

  console.log(re);
  return re.body;
}, "fetchData");

This would work just fine and is in-tune with every spec implementation of the Fetch API.

Our premise in Solid is to keep a light abstraction on top of the platform (as little compilation as possible) and protect security best-practices.


Additionally, I'd possibly question the necessity of making an actual fetch request from one server function to another, not really sure I understood the use-case here.

@huseeiin
Copy link
Contributor Author

huseeiin commented Feb 1, 2025

I believe accepting relative paths aren't a good enough reason to patch prototype or introducing a compile-time change.

"http://localhost:3001/api" is not good because that is a network call.

and about your other point, reasons why you may want to call fetch() in a server function:

  1. migrating an old backend
  2. exposing a unified api that your frontend and other frontends can use

and this doesn't need compilation, it is a runtime patch. and solidstart already has nitro's $fetch btw, but its broken in solidstart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants