Ajax request vs routing

hi there,

i’m dynamically loading subpages of another page via ajax and a content representation (including a snippet) on the home page, passing the relating subpage-uid via url parameters.
now i redirect the urls like:
config.php

<?php

return [
'debug' => true,
'routes' => function($site) {
    return [
        [
            'pattern' => '(:all)?(:all)',
            'action' => function ($slug) {
                $data = [
                    'current' => $slug,
                ];
                if (site()->find($slug)) {
                    return page('home')->render($data);
                } elseif (page('projects/' . $slug)) {
                    $page = page('projects/' . $slug);
                    return page('home')->render($data);
                } else {
                    return site()->errorPage();
                }
            },
            // 'filter' => function($slug) {
            //     if(Str::contains($slug, 'json')) {
            //         return false;
            //     }
            // }
        ],
        [
            'pattern' => '/projects/(:any)',
            'action' => function($uid) {
                go($uid);
            }
        ]
        ];
}
];

without ajax, the links do work as intended (and reload the whole page). but after adding the route, the parameters seem to get lost in the get request. how do i bypass the route for json requests? (see commented part)

or am i maybe thinking too complicated? the goal is to simply have projects accessible by url like “example.com/exampleproject” and on ajax load pushstated, so the address bar is always acting “normal” while the page stays the same.

thank you!

Not sure if $this->next() works here, but try:

  [
            'pattern' => '(:all)?(:all)',
            'action' => function ($slug) {
                $data = [
                    'current' => $slug,
                ];
                 if(Str::contains($slug, 'json')) {
                   $this->next();
                 }
                if (site()->find($slug)) {
                    return page('home')->render($data);
                } elseif (page('projects/' . $slug)) {
                    $page = page('projects/' . $slug);
                    return page('home')->render($data);
                } else {
                    return site()->errorPage();
                }
            },
          
        ],

Otherwise, exclude routes that contain json in your pattern, using a regex pattern.