question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect with hashtag from server gets stripped in InertiaJS

See original GitHub issue

Versions:

  • @inertiajs/inertia version: 0.9.1
  • @inertiajs/inertia-vue version: 0.6.1

Describe the problem:

I have a Laravel app, where I want to redirect with a hashtag (e.g. example.com/page#hash) but the hashtag gets stripped.

It seems this is the line preventing hashtags to come along:

https://github.com/inertiajs/inertia/blob/master/packages/inertia/src/router.ts#L275 https://github.com/inertiajs/inertia/blob/211f49b98928b0f5f366387b6fbf99a65e4a07be/packages/inertia/src/url.ts#L42

Is it possible to allow hashtags? Might be cool if this would be configurable, like:

this.$inertia.post('/url', {foo: bar}, {allowFragments: true});

Steps to reproduce:

In a Laravel app, do:

return redirect()->route('my-page', '#hash');

Or:

return redirect()->route('my-page')->withFragment('hash');

And see results in InertiaJS, in default Laravel + Blade it works fine, the hashtag remains in the URL.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
claudiodekkercommented, Jan 20, 2022

Not right now, that’s what the issue is about 😉

1reaction
aidan-caseycommented, Jun 9, 2021

In a traditional Laravel + Blade app, the server returns a redirect response with a Location header. The browser then sends the request to the URL specified in the Location header (which would include the URL fragment). There’s nothing funky going on here and it is working as the browser should.

In the case of Inertia, it’s not being made aware of the Location URL (with the hash) because it is utilizing Axios under the hood which is just following the redirect without making Inertia aware of the fragment in the destination URL. Inertia is then updating the current URL and browser history with the URL in the JSON Inertia response from Laravel (which doesn’t include the fragment).

In order to get this working, Inertia technically needs to intercept the redirect response, store the hash fragment client side, make the request to the destination, and finally append the hash back onto the URL once done as it does with normal visit requests without a redirect.

Edit: @RobertBoes did some further digging on this and actually found we wouldn’t even be able to intercept the redirect client-side to read the Location header. This is according to specs for XMLHttpRequest and fetch. This essentially leaves us without much ability to solve the underlying issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to keep hash in redirect url Laravel 8 and Inertiajs
I'm trying to redirect to a url with a hash on the end of the url, and for some reason no matter how...
Read more >
How To Redirect Back In Inertia.Js - ADocLib
Repository from Github https://github.com/inertiajs/inertia.MIT License 3495 55 417 207 Redirect with hashtag from server gets stripped in InertiaJS.
Read more >
Redirect with /link#deeplink - Laracasts
When I use redirect()->back(); in my controller, the #element gets stripped off my ... The url fragment (hash) doesn't get transmitted to the...
Read more >
how to redirect from with in a react method Code Example
function that redirects to another page react ... how to get the url page is redirected from javascript · react router redirect. url...
Read more >
291: All Things Inertia.js with Jonathan Reinink – The Bike Shed
It's like, no, that's all just server-side now in one place. The other amazing thing you get is you mentioned redirects and that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found