Prevent users visiting 'template' of list detail pages

What’s the best way to prevent people to access the url of the template for our list detail pages?

For example:

I have a list detail page for more information about courses. Every course then has it’s own list detail page, like /course/example1 and /course/example2. But when someone directly goes to /course it shows the default template we’ve set up in Softr.

Is there a way to prevent this?

(Preferably without custom code)

1 Like

The only way I know how to handle this is with custom code. What this code does is looks at the URL and if it does not find a record ID, replaces the current page with one that has a record id for a default record. You could redirect anywhere, though.

<script>
if (!window.location.href.includes("/r/rec")) {
    const urlParams = new URLSearchParams(window.location.search);
    const recordId = urlParams.get('recordId');
    if (!recordId) {
        urlParams.set('recordId', "rec123")
        window.history.replaceState({}, '', `${location.pathname}?${urlParams}`);
    }
}
</script>
3 Likes

Thanks David.

I hope Softr comes up with a native solution for list detail pages, or a workaround with redirects, but otherwise your script might help.

However I can’t code… If I want to redirect those URL’s to the homepage, which elements do I have to change in your script?

Something more like this:

<script>
if (!window.location.href.includes("/r/rec")) {
    const urlParams = new URLSearchParams(window.location.search);
    const recordId = urlParams.get('recordId');
    if (!recordId) {
        window.location = "https://example.com";
    }
}
</script>

Where “https://example.com” is replaced by your home page.

You would put this script in the Code inside header section of your page settings.

(I edited this post on 29 Sept 2022 because the original script was wrong.)

2 Likes

This is something that could be added into the platform by default. The reason it’s not yet added is that we feel sometimes the app builder might be just going into the page to check how it looks like :slight_smile: Perhaps we should handle it similarly to 404 or 401. Still thinking :slight_smile:

Thanks @artur.

That sounds good.

Or perhaps you could also an option to the block, where people can set a redirect for the default page (or how do we call this?)

Thanks @dcoletta, appreciate your help.

Do I have to add this script in the header of the list detail page?

I tried that and published the app again, but it still shows the default page to me (even after clearing cache)

Can you share the link to your detail page ?

Sorry! I should not have posted that script without testing it. I have fixed the post above and it should work now.

2 Likes

Great, it works now. Thanks David :slightly_smiling_face:

1 Like