SolidJSS
SolidJSโ€ข3mo agoโ€ข
2 replies
chanon_s

How do I get the host or full request url inside a "use server" function?

For example, maybe I have this:

// src/routes/posts/[id]/index.tsx
import { ErrorBoundary } from "solid-js";
import { query, createAsync, type RouteDefinition } from "@solidjs/router";

const getPost = query(async (id: string) => {
  "use server";
  const post = await fetch(`https://my-api.com/posts/${id}`);
  return await post.json();
}, "post");

export const route = {
  preload: ({ params }) => getPost(params.id),
} satisfies RouteDefinition;

export default function Page() {
  const postId = 1;
  const post = createAsync(() => getPost(postId));
  return (
    <div>
      <ErrorBoundary fallback={<div>Something went wrong!</div>}>
        <h1>{post().title}</h1>
      </ErrorBoundary>
    </div>
  );
}


the issue is at
  const post = await fetch(`https://my-api.com/posts/${id}`);


The https://my-api.com part needs to change according to the current host of the request (eg. development would be localhost, staging would be somewhere else etc.)

How can I access that within the getPost function?

window is not available so I can't do window.location.hostname

It would also be helpful to be able to access the full request url in there
Was this page helpful?