Using optional parameters
Since we teased optional parameters in the Creating Dynamic Pages section of Chapter 4, let’s start there. When creating optional parameters in a route, there are some things to keep in mind. For instance, they cannot exist alongside another route as this would cause a collision in the routing rules. When creating an optional route, it works best for instances where the final portion of the route can have a default option. Many applications will change the URL based on a language selected by the user. For our example, we’ll illustrate how to create an optional parameter by selecting a country in North America that our demonstration store operates in. We won’t actually build an entire store but rather use it to illustrate the advanced routing concepts in this chapter.
To begin, let’s create a new route in our navigation just like we have for previous examples:
src/lib/Nav.svelte
<nav> <ul> ...