NextJS specific dynamic routes - next.js

I'm building a small application and want to build a simple routing system but I got into a small issue.
I have my category page which is dynamic, and a single post which will be category/post-name now I want that my category page to be only accessible when the user goes to website.com/category1/ or website.com/category2/ or website.com/category3/ but not on other dynamic routes.
Is there a way to do it?

Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. For example:
pages/category/category1.js - Will match /category/category1
pages/category/caregory2.js - Will match /category/category2
pages/category/category3.js - Will match /category/category3
pages/category/[category_id].js - Will match /category/category4, /category/category5, etc. But not /category/category1, /category/category2, /category/category3.

Related

how to handle nested level dynamic routing in Nextjs

I have to implement 3 dynamic page routes in nextjs App.
/blog/category/postname
/blog/category
/blog/postname
I can implement 1 & 2 routes as dynamic routes but the problem is with the 3rd one. Actually, I need 3rd one because the production site is having the same slug for some of the posts. so want to maintain the same slugs in my nextjs application as well. Is there any solution to implement this??
You have an example here.
In your /pages:
You create blog folder.
Case 1 (excluding root): In blog, you create [...slug].js (or .tsx)
Example: /blog - ERROR ;/blog/category - OK; /category/postname - OK
Case 2 (lot of levels, including root): In blog, you create [[...slug]].js (or .tsx)
Example: /blog - OK;/blog/category - OK; /category/postname - OK
You handle all logic inside this file.
You can check my answer to understand how routing is done.

Creating static paths with multiple constrained params

I'm now in the process of developing my small project and I'm not sure if it's even possible to do this the way I will describe below, so... I have dynamic routes like "/[community]/[townName]". How can I generate static paths where [townName] is constrained to [community]?
In other words - let's say we have townName "abc1". This town is in community "xyz1" so the page /xyz1/abc1 should be accessible and NOT throw 404. But there is also town with the same name "abc1" in "xyz2". So the path /xyz2/abc1/ should also be accesible.
However there is no town with same name in community xyz3 so I do not want to generate page for /xyz3/abc1/ - user should see 404 error.
Of course each town has it's unique ID in database and I could use it to generate pages, but I want my url to be SEO friendly.
All help and tips are appreciated. Thanks!
You should check getStaticPaths and dynamic routes from official Next.js website. It has more than one way to do what you want but there are additional customization options.

Dynamic Routing NextJS with optional paths

Here is the issue I am trying to solve with creating dynamic routes in NextJS.
-products
   -[category]
       -[size]
          -[product]
The issue is some products don't have a size parameter and would need to bypass this parameter to get to the product landing page. Here is an example of the different routes I would like to have:
/products/ceiling-lighting/2x4/light-one
/products/ceiling-lighting/2x4/light-two
/products/ceiling-lighting/2x2/light-one
/products/ceiling-lighting/2x2/light-two
/products/light-switch/switch-one
/products/light-switch/switch-two
Is this possible to do with dynamic routes?
Nope it will not work, you have to pass required number to parameters/segments to reach that specific dynamic page, but as juliomalves said, you can use catch all routes, after using this you will get your parameters in the form of array and you can navigate programmatically depending on number of parameters or whatever logic you want.

Google Analytics for one site with language specific domains

I have one site that serves EN and FR users. Each language has it's own domain, so englishcontent.com and frenchcontent.com. I'm setting up profiles, one for each. For the filters, would this be the correct setting
Type: Custom > Include
Field: Hostname
Pattern englishcontent.com
I would then have a profifile for French with the similar settings.
is this the best way to do this?
Yes, it should work, but keep in mind keep always a view without filter, in you case you need to have 4:
EnDomain View
FRDomain View
AllDomain View (No domain filter, but ip filter or any other needed)
Raw View (No Filter at all , this in case of something fails)
Take care of the variation of the domains and your filter. For example whats happens with the www variations or the subdomains.
Greetings
Example
PS: This structure is not the recomended one by Google, it suposes one account per brand, one property per domain and the desired views.
more information on:
https://analyticsacademy.withgoogle.com/course/1/unit/4/lesson/2

How many controller files should I create in my Symfony2 application?

In the src/AppBundle/Controller folder, there is a file called DefaultController.php.
I'll create url's like below, should I use just DefaultController.php for all URL requests or is it recommended to use a different controller.php file (UserController.php, FeedController.php, etc) for each part of the site? (profile, feed, settings, etc)
I also have another question. As far as I understand, we put our html files in our /App/Resources/views folder to keep them separated. Do I need to create a specific file for each part of the website just like flat PHP? (settings/index.php, settings/password.php, settings/things.php, settings/security.php, etc).
I am not sure whether this question is suitable for SO or not.
settings
/settings
/settings/password
/settings/things
/settings/security
/settings/privacy
/settings/ban
/settings/notifications
/settings/mail
/settings/mobile
/settings/applications
/settings/advertising
/settings/invite
user
/username
/username/photos
/username/friends
/username/posts
feed
/feed
/feed/posts/postid
For both questions is no hard answer. I should create a controller for each part of your website AT LEAST. Theoretical you could throw everything into one controller but it will be a very long list if you are finished. Another problem is that your action names like indexAction will repeat which is of course not possible because every method must have a different name. And names like index1Action, index2Action and so on is also not a proper solution :-). Another helper is to create an own controller for every ENTITY.
Twig files should only be written for one page only or only for a part of a page. Imagine that you have a homepage with last 10 newsitems but also a news page with more news items (maybe with pagination). The newsitems themselves looks the same on both pages. In this case you could make a home.html.twig, a news.html.twig and also a newsitem.html.twig. Both home and news will include newsitem to show the newsitems...
Hope i gave you a light.

Resources