I would like to route a request that looks like this:
https://sub.domain.nl/tracking/MM-123/tracker.js
To my javascript in this directory:
Resources/js/Scrips/tracking/tracker.js
However, it looks like the server/laravel is looking for this script:
https://sub.domain.nl/tracking/MM-123/tracker.js
In the /public folder.
However, I want it load it just like a normal view / page from the server.
My web.php router:
Route::group([], function () {
Route::get('/tracking/{script}/tracker.js', [TrackingScriptController::class, 'index'])->name('tracking');
});
My TrackingScriptController.php:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesResponse;
use IlluminateSupportFacadesFile;
class TrackingScriptController extends Controller
{
public function index($script)
{
// Construct the path to the script file
$filePath = resource_path("js/Scripts/Tracking/tracker.js");
// Check if the file exists
if (!File::exists($filePath)) {
// You can return a 404 or some other error if the file is not found
abort(404, 'Script not found');
}
// Read the contents of the file
$scriptContents = File::get($filePath);
// Return the contents with the correct Content-Type header
return Response::make($scriptContents, 200, ['Content-Type' => 'application/javascript']);
}
}
?>
But the browser keeps giving 404 page not found, in console:
Failed to load resource: the server responded with a status of 404 () tracker.js
So it looks like my laravel app is trying to look for this .js file inside of the public app. When I remove .js from the URL like this:
https://sub.domain.nl/tracking/MM-123/tracker
And my web.php to this:
Route::group([], function () {
Route::get('/tracking/{script}/tracker.', [TrackingScriptController::class, 'index'])->name('tracking');
});
It works fine.
I’m using Laravel (Breeze starter kit) + Inertia + Vue.
Does anyone have an idea how to fix this? I have no idea unfortunately after trying a lot of different things.