Changing routes for JS SPA?

Hi,

I’m embedding a Javascript single-page-app within my Kirby site, and I need to stop the PHP from rerouting when it reaches certain pages.

For example:
The app is located at domain.com/tools, so I need anything after tools to redirect to /tools.

domain.com/tools/* -> domain.com/tools

Any resources I can look at, or recommendations? Thanks.

Based on the docs (https://getkirby.com/docs/developer-guide/advanced/routing) I managed to write this in my config.php

c::set('routes', array(
  array(
    'pattern' => 'tools/(:any)',
    'action'  => function() {
      return go('tools');
    }
  )
));

It works, but it always redirects to the same “tools” page and not to any of the Javascript routes. I’ll keep working on it.

Note: If you can’t get this to work, make sure your routes don’t have a slash at the beginning (/tools) but just the name (tools).

Got it!

I needed the “page” function (which returns a different page), not the “go” function (which redirects).

c::set('routes', array(
  array(
    'pattern' => 'tools/(:any)',
    'action'  => function() {
      return page('tools');
    }
  )
));