-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Unexpected behaviour when redirecting in action #2997
Comments
When a loader/action returns a redirect from a client-side navigation, Remix uses status 204 and a header remix/packages/remix-server-runtime/server.ts Lines 135 to 149 in a2e1449
|
Okay that makes sense but in that case it seems to be bypassing If Remix needs to amend the response status should it not do it after |
|
But the point is that Would this not be a simple change in
to:
|
I just ran into this issue but on Microsoft Edge only; everything worked fine on Chrome and Firefox. I am redirecting on an Action submission (passing OAuth info to the backend) and I can see both the const submiter = useFetcher();
const [searchParams] = useSearchParams();
const redirectTo = searchParams.get('redirectTo') ?? '/actions';
const navigate = useNavigate();
useEffect(() => {
if (!isBrowser || submiter.type !== 'init') return;
const formData = new FormData();
const contentHash = window.location.hash;
if (hashContainsKnownProperties(contentHash)) {
const hash = getDeserializedHash(contentHash);
if (hash.error) {
formData.append('error', hash.error);
} else {
Object.entries(hash).forEach(([key, value]) =>
formData.append(key, value as string),
);
}
window.location.hash = '';
}
formData.append('redirect_to', redirectTo);
submiter.submit(formData, {
action: '/oauth/callback',
method: 'post',
});
}, [submiter, redirectTo]);
useEffect(() => {
if (submiter.type === 'done') {
navigate(redirectTo, { replace: true });
}
}, [navigate, redirectTo, submiter.type]);
return null; |
I seem to have the same issue with the Update: Update 2: I think I managed to pinpoint my own issue, I don't know if this is related to the original issue of this thread, but remix-auth-github properly redirects when |
@fknop I had the same issue - lots of weird things happening with export const LiveReload =
process.env.NODE_ENV !== 'development'
? () => null
: function LiveReload({
port = Number(process.env.REMIX_DEV_SERVER_WS_PORT || 8002),
nonce = undefined,
}) {
let js = String.raw;
return (
<script
nonce={nonce}
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html: js`
(() => {
let protocol = location.protocol === "https:" ? "wss:" : "ws:";
let host = location.hostname;
let socketPath = protocol + "//" + host + ":" + ${String(
port,
)} + "/socket";
let ws = new WebSocket(socketPath);
ws.onmessage = (message) => {
let event = JSON.parse(message.data);
if (event.type === "LOG") {
console.log(event.message);
}
if (event.type === "RELOAD") {
console.log("💿 Reloading window ...");
window.location.reload();
}
};
ws.onerror = (error) => {
console.log("Remix dev asset server web socket error:");
console.error(error);
};
})();
`,
}}
/>
);
}; |
The old version of LiveReload solves my issues as well. I believe it breaks due to this added block: ws.onclose = (error) => {
console.log("Remix dev asset server web socket closed. Reconnecting...");
setTimeout(
() =>
remixLiveReloadConnect({
onOpen: () => window.location.reload(),
}),
1000
);
}; More specifically, I think the arbitrary 1000 ms timeout before reloading creates a race condition, where, during a redirect, the route you're navigating from will reload itself before the redirect has been able to complete. By increasing the timeout, the issues disappeared for my part, but that does not seem like a viable permanent fix. |
Hello, I have tested this behaviour with last remix version as of now ( I am running a 2020 Macbook Air with M1 Proccesor (big sur, 11.6 os version( and, weirdly, this bug happens only on Firefox (version 104.0.2). On Chrome and Safari, redirects work ok. I tried replacing the |
* Get basic auth running with errors * add login method * logging * add register endpoint * stop using forked version of form * testing error states * fix dev settings * fix up error handling and add some new methods * Add additional auth utilities * add schema to response * begin adding join route * rename CmsAuth * remove logging * begin adding some cms actions * begin adding event fetching * Add ts packages * use tsconfig # Conflicts: # tsconfig.json * remove some comments * add remix type defs * update server.js to match remix example * convert entry files to ts * use ts * allow redirect on success * add event forwarding * query all calendars * display message * move auth into membership route * use ts * add route protection back in to dashboard * return value from loader * move files into frontend route * update paths from rename * Move app files into __app * Add tailwind * remove tailwind.css from vc * ignore tailwind * add spread params to svgs * move around auth routes and add styles * rename dashboard to membership index * adjust styles * move join into auth * remove old thing * renive extra links * fix up auth * add getCalendars * move auth into pathless * move layouts into correct folder * use new singletask layout * convert to ts * use updated types * add first pass of events * add inter font * use classNames * rename method * add events calendar * add events calendar views * description * update calendar formatting * add popover for event * remove console.log * go stacked layout * adjust for stacked layout * add meta data * begin adding more components and details * Add ts versions * move files * reset dependencies * check for null data * fix some ts issues * update dependencies * update utility types * update heroicons * abstract some components * clean up styling * use sky instead of indigo * upgrade remix dependencies and scripts * add tiny-invariant * convert to ts * allow for netlify dev * use globals * open in new tab * continue debugging * auth debugging go away * ignore * convert to ts and wrap in global * split out cms functions * removing logging * check for user * move tls reject to env variable * read user via getUser * removing * clean up events * authenticate * use split versions * remove esbuild from node bundler * turn on defaults * turn off esnext for now * update login form * add note * fix this thing * remove console.log call * use custom livereload from 1.6.4 of remix see remix-run/remix#2997 (comment) * remove debugging help * move file * fix faker fn * tweak Button styling and add fullWidth prop * Add TextInput and FieldGroup * style reset password form * clean up auth forms * convert to ts * convert some ts * add wide option * add FieldSet, TextArea, and addons for TextInput * clean up register * fix up register form / hide form for now * use solid icons * Abstract events list and add ics link * use updated imports * add events to dashboard * turn on esnext * correct link address * remove comment * add getCalendar * add black and white buttons * Add passedRef prop * Add subscribe links * move join files * fix links * fix import * Fix path for mdx loading * abstract out not found content * Add splat page to catch non-loader 404s * do not default to empty string * remove extra h1
I confirmed that |
Thank you @ErickTamayo I've spent like 2 hours going nuts why my redirect is not working, I can also confirm that it only happens in Firefox and if I remove |
As mentioned before this was introduced in #859 to enable automatic re-connection when the dev server is restarted. Would it be possible to check for the close event code before setting the timeout? This would keep the auto reconnect behavior while fixing the Firefox behavior (at least in a quick test): - ws.onclose = (error) => {
+ ws.onclose = (event) => {
console.log("Remix dev asset server web socket closed. Reconnecting...");
- setTimeout(
+ if (event.code === 1006) setTimeout(
() =>
remixLiveReloadConnect({
onOpen: () => window.location.reload(),
}),
1000
);
};
|
I've also done a quick test with @danieljb's |
Thanks guys! I had a headache for 2 days straight because I couldn't find the issue... Any PRs happening for this? |
I believe PR 4725 fixes this issue. |
I'm testing a redirection while authenticating users and the POST request is not triggering the action so the redirection is not happening until the user refresh the page. I'm using RemixJS 1.11.0 and React 18.2.0. Please check the current implementation: const csrf = useAuthenticityToken();
const { web3AuthState } = useWeb3AuthContext();
const submit = useSubmit();
useEffect(() => {
if (web3AuthState.state === 'connected') {
console.log('Logged in with Web3Auth')
submit(
{ ...web3AuthState, csrf },
{ replace: true, method: 'post' }
);
}
}, [web3AuthState]); I added logs from the action of this route but they are not called at any time from Firefox 🤔 Any help is really appreciated! |
While the useSubmit hook is fixed, I was able to solve this wrong behavior by using a hidden form element directly: const formRef = useRef<HTMLFormElement>(null);
useEffect(() => {
if (web3AuthState.state === 'connected' && formRef.current) {
console.log('Logged in with Web3Auth')
formRef.current?.submit(); // Fixed by replacing useSubmit in order to trigger the submit of the auth redirection
}
}, [web3AuthState, formRef]); Let me know what you think folks! <3 |
Hum, I feel like a lot of issues have been mixed into this one. |
This issue has been automatically closed because we haven't received a response from the original author 🙈. This automation helps keep the issue tracker clean from issues that are unactionable. Please reach out if you have more information for us! 🙂 |
@machour can you reopen this issue please? |
During my tests with useSubmit, I'm getting a 201 status code instead of a 302 redirection. can you validate that wrong behavior? This is the code of that action: export const action = async ({ request }: ActionArgs) => {
try {
// validations here, redirecting users with this action submit :)
return redirect('/profile',
{
status: 302,
headers: {
'Set-Cookie': await saveLoginCookie(),
}
}
);
} catch (error) {
console.error('Auth error', error);
return json(`Sorry, we couldn't authenticate the user`, {
status: 500,
});
}
}; |
I am new to Remix. Just started last week. I am using latest indie stack. After I build the app for production, and run it in production mode using Safari: After login it will not redirect to the Notes page.Chrome: After login redirecting to Notes page just fine.My observation: Safari seems to be not redirecting due to secure: true turned on in the createCookieSessionStorage setting. export const sessionStorage = createCookieSessionStorage({
cookie: {
name: "__session",
httpOnly: true,
path: "/",
sameSite: "lax",
secrets: [process.env.SESSION_SECRET],
secure: process.env.NODE_ENV === "test", // won't work when its set to production
},
}); Let me know what I can do to improve? |
Hey folks! As @machour noted this issue seems to now have split into at least 3-4 different potential issue discussions so it's near impossible to continue triaging. I'm going to close this out and ask the following:
Thank you! This will make it much easier for us to debug any potential issues. |
What version of Remix are you using?
1.4.1
Steps to Reproduce
Create a Remix project (with the built in Remix server) with the following routes:
index.tsx:
magic-link.tsx:
verify-magic-link.tsx:
In the
entry.server.tsx
handleRequest
function put the following just before returning the response:In the
entry.server.tsx
handleDataRequest
function put the following just before returning the response:Go to
https://localhost:3000/magic-link?code=123456&[email protected]
in your browser. In the terminal you should see:This is what I would expect to see.
Now in
verify-magic-link.tsx
changeconst verifyMagicLink = ({ email, code } : { email: string, code: string }) => Promise.resolve(false)
toconst verifyMagicLink = ({ email, code } : { email: string, code: string }) => Promise.resolve(true)
.Run the same request in your browser again.
Expected Behavior
Terminal should show:
Actual Behavior
Terminal shows:
The
POST
request does not get logged by eitherhandleRequest
orhandleDataRequest
.Even though I am taken to the
index.tsx
route as expected the302
redirect response never seems to be returned to the browser. Instead when I look in the browser network tab I can see that the the response to thePOST
request was a 204 response.This is causing me issues because I would like to add a
set-cookie
header on the redirect response but the cookie never gets set because the redirect response doesn't make it to the browser.The text was updated successfully, but these errors were encountered: