ash-jc-allen / short-url

A Laravel package for creating shortened URLs for your web apps.
MIT License
1.25k stars 157 forks source link

Click even t(ShortURLVisited) only registering once on live #199

Closed HeadStudios closed 1 year ago

HeadStudios commented 1 year ago

Any idea why my ShortURLVisited event is only triggered once (the first time) the URL is clicked but not others? Is there anything I should check? Thank you

my click event: https://gist.github.com/HeadStudios/b2ffd252e82be5f80a2b0085bedf7057

event service provider https://gist.github.com/HeadStudios/73e78099a8bff75733d83f6c37d3a899

Thank you! Video for context: https://p147.p4.n0.cdn.getcloudapp.com/items/o0uYr8xd/5e74cd1b-4289-4110-87d8-539872cdbbe2.mp4

Note: it does seem to work if I open in Incognito tab and go to the short url - just strange behavior that's not happening on local and I'm trying to discover and investigate why. Any input much appreciated thank you for your review.

HeadStudios commented 1 year ago

Even when the route is cleared it's still being cached so strange :(

https://p147.p4.n0.cdn.getcloudapp.com/items/jkuAzZWY/43392d23-b403-4ed4-bef9-3793c4fba339.mp4?v=01996e85e759b8eb0cb96b32ec6b9581

ash-jc-allen commented 1 year ago

Hey @HeadStudios! Sorry that you're having issues with the package :(

I have a hunch about what could be causing this. Would you be able to confirm what redirect status code your short URLs have in the database?

By default, the short URLs that are created are given a 301 status code. So the first time you visit your short URL, you will hit your Laravel app and be redirected to the destination URL. But the browser will remember this destination URL, so next time you try and visit the short URL, your browser will redirect you straight to the destination URL. This means you'll never hit your Laravel app after the first time you visit it (in the same browser).

First visit:

Short URL -> Laravel app -> Destination URL

Second, third, forth, etc visit:

Short URL -> Destination URL

If you'd like to hit your Laravel app on every request, you could set your redirect status code to be 302 instead (https://github.com/ash-jc-allen/short-url#redirect-status-code).

As for why it works on your localhost, I'm not entirely sure. I wonder if the browser doesn't cache 301 redirects for localhost because it knows it's probably being used for development and that sort of thing? 🤔

Hopefully, this solves your issue? But please give me a shout if it doesn't work though 😄

HeadStudios commented 1 year ago

Hi Ash,

Appreciate your quick and detailed response.

I can confirm that adding the 302 redirect has fixed my issue.

You're the man!

Closing issue.