How to enforce trailing slash on loading page

I would like to be able to enforce a trailing slash on all pages of my website, because pages that don’t have it will break (due a redirect from a subdomain). I have tried with JS using some of these suggestions: javascript/jquery add trailing slash to url (if not present) - Stack Overflow

However, I am not even sure whether my script is enforced. When should it be called in the page and how can I make sure it is actually enforced?

can’t you do that with the permalink? all my jekyll sites have always had trailing slashes on all pages.