Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vernancheta25's avatar

JavaScript file is triggering route middleware

I don't know what's going on but for some reason a JavaScript file is triggering my route middleware.

Here's my code:


Route::middleware(['record_linklistview'])->group(function() {

 
  Route::get('/{linklist_url}/{page_id?}/{page_id2?}/{page_id3?}/{page_id4?}/{page_id5?}', 'LinklistController@index');

});

And here's my middleware:

namespace App\Http\Middleware;

use Closure;

class RecordLinklistView
{
  
    public function handle($request, Closure $next)
    {

         $linklist_url = $request->route('linklist_url');
         info('url: ' . $linklist_url); // this is logging js for some reason
     }
}

So when I access the page, the JavaScript file in that page is also triggering the middleware which results in an error. It's just an ordinary JavaScript file compiled with mix:

<script src="{{ mix('js/LinkListRoot.js') }}"></script>

Interestingly, it doesn't get triggered when I access the file manually on the browser.

In my controller, I have it like so. Which made me think something must be going on in the controller which makes the JavaScript file get triggered:

public function index(FormatLinklistRequest $request, $linklist_url, $page_id = null, $page_id2 = null)
  {
    $linklist = $request->linklist;

    return $this->getLinkListPage([
      'linklist' => $linklist,
      'link_url' => $request->path(),
      'request' => $request
    ]);
  }

But even if I simplify it like this, the JavaScript file is still triggering the middleware:

public function index(Request $request)
{
    $linklist = $request->linklist;
    return view('tester');
}

Here's the simplified view. The JavaScript file is triggered for both simplified code and the current code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>hello</h1>
    <script src="{{ mix('js/LinkListRoot.js') }}" defer></script>
</body>
</html>

My http kernel file looks like:

protected $routeMiddleware = [
   'record_linklistview' => \App\Http\Middleware\RecordLinklistView::class,
]

I'm out of ideas. Please help.

0 likes
1 reply
Sinnbeck's avatar

Can you show the error? And how do you know it isn't something in the file that triggers it? Does it trigger if the file is empty?

Please or to participate in this conversation.