Add helper to detect client-fetch vs. full-page-load? #311
maranomynet
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
You can use fetchDest to detect that. import { fetchDest } from "remix-utils/sec-fetch";
export async function action({ request }: ActionFunctionArgs) {
let data = await getDataSomehow();
// if the request was made with JS, we can just return json
if (fetchDest(request) === "empty") return json(data);
// otherwise we redirect to avoid a reload to trigger a new submission
return redirect(destination);
} The return value of This is safer than using the Accept header because the Sec-Fetch-Dest header can't be customized, is set by the browser, while you could do fetch("/url?_data=ROUTE_ID", {
headers: { accept: "text/html" }
}) Remix ignores the accept header and use the |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi @sergiodxa. Would you be open to accepting a PR for a helper function that answers the question whether a loader/action is running because of a scripted fetch navigation/submit action or is caused by a normal page load (initial browser request, reload button or
curl
command)?What I'm thinking of is something like this:
Beta Was this translation helpful? Give feedback.
All reactions