How to set the default route #30
-
First - Pinecone looks like it will be really useful - thanks for putting it together. I'm trying to work out the best way to define a default route for a page. Say I have an Alpine component with three routes:
When I view the page, all the templates are hidden. I understand why that is, but I want the first one (with route '/') to be shown by default, and also when I click the 'Home' link. I am able to do this by adding an x-init on the component:
Is this the best (or only) way to have a template with an x-route attribute displayed immediately? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hey! or do you mean the template take long to show? in that case wouldn't be the same delay for all page is or is it just the |
Beta Was this translation helpful? Give feedback.
-
OK, I see why this is happening. Here is the code for the page. The pages are served from localhost:5500 (I'm using Live Server from VS Code)
Question, though - can I use a regex in the x-route directive? So I can match an optional |
Beta Was this translation helpful? Give feedback.
-
I think the best approach would be not have index.html in the URL at all. That's not how client side routers are meant to work.
However if you're forced to use it, this may be a better approach: <template x-route="/index.html" x-handler="ctx => ctx.redirect('/')"></template> This will make it so it only redirect to Glad you were able to figure out and thanks for using the router! |
Beta Was this translation helpful? Give feedback.
I think the best approach would be not have index.html in the URL at all. That's not how client side routers are meant to work.
Take this for example: https://reactrouter.com/index.html
It also shows 404. And it's the most used framework and router, so it's the standard.
and no you can't use regex only options are mentioned in the README.
/index.html
is inconsitant cause then shouldn't the rest of the routes be/index.html/about
or even/about.html
?However if you're forced to use it, this may be a better approach:
This will make it so it only redirect to
/
when you view/index.html
route.Your method in
x-init
…