solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.14k stars 146 forks source link

useSubmission fail to return a response (result) since v0.11.4 #370

Closed Ashyni closed 7 months ago

Ashyni commented 8 months ago

Describe the bug

In a solid-start project, useSubmission() fail to return a response from the POST request since "@solidjs/router": "0.11.4"+. The error is different on stackblitz (failing anyway). "@solidjs/router": "0.11.3" is the last working version. Not sure how to investigate further.

Your Example Website or App

Working repro: https://stackblitz.com/edit/github-rv6qqk-k6vbnx Failing repro: https://stackblitz.com/edit/github-rv6qqk-tmynpf

Steps to Reproduce the Bug or Issue

// index.tsx
import { action, useSubmission } from '@solidjs/router';
import { Show } from 'solid-js/web';

export default function Home() {
  const submission = useSubmission(login);

  return (
    <main>
      <form method="post" action={login}>
        <label for="username">Username:</label>
        <input name="username" id="username" />
        <br />
        <button>Continue</button>
        <Show when={submission.result}>
          {(result) => <p>{result().message}</p>}
        </Show>
      </form>
    </main>
  );
}

const login = action(async (formData: FormData) => {
  'use server';
  const username = formData.get('username');
  if (
    typeof username !== 'string' ||
    username.length < 3 ||
    username.length > 31 ||
    !/^[a-z0-9_-]+$/.test(username)
  ) {
    return new Error('Invalid username');
  }
});

Expected behavior

Same as the previous working version.

Platform

Ashyni commented 7 months ago

Found a workaround, explicitly set singleFlight to false (router 0.12.0), true by default. https://github.com/solidjs/solid-router/blob/76b2689633f40baf32ae23834df3478e4589afe1/src/routing.ts#L342

https://stackblitz.com/edit/github-rv6qqk-v9aih9?file=src%2Fapp.tsx

// app.tsx
// @refresh reload
import { MetaProvider, Title } from "@solidjs/meta";
import { Router } from "@solidjs/router";
import { FileRoutes } from "@solidjs/start";
import { Suspense } from "solid-js";
import "./app.css";

export default function App() {
  return (
    <Router
      root={(props) => (
        <MetaProvider>
          <Title>SolidStart - Basic</Title>
          <Suspense>{props.children}</Suspense>
        </MetaProvider>
      )}
+     singleFlight={false}
    >
      <FileRoutes />
    </Router>
  );
}
ryansolid commented 7 months ago

This is a SolidStart issue. I missed a code path and it was eating the responses to server actions when route load functions weren't present. This commit should fix it. It will be updated in the next version of start(https://github.com/solidjs/solid-start/commit/38ba8de8e0758dc622342332d32cd9a88f6cf52a). Until then yeah turn singleFlight off.