NextJS & Prismic - How to prevent /:uid from rendering homepage - next.js

In the prismic docs the default setup they teach means that your homepage and every other page on your site is the same type.
This means that in that default setup the routes / and /home both render the homepage.
Is there a way to prevent the /home route from rendering, or at least redirect it to /?

The easiest way to do this is just with a standard redirect, in your next config:
https://nextjs.org/docs/api-reference/next.config.js/redirects
But there are some other ways that might work, too. For instance, you could add a if(params.uid === "home") router.push("/") to pages/[uid].js, to redirect users from /home to /. (I don't think that code snippet would work, but I hope it illustrates the concept.)
Let me know if you want some other ideas, or if you need help with implementation! I'd be happy to put together a proper example if you need :)
Sam
Edit: Another option would be to create a singleton "Homepage" Custom Type in Prismic.

Related

nested pages in strapi and nextjs

So we want to create a big website with nextjs and strapi.
We sometimes have deep nested pages like
www.ourwebsite.com/onderwijs/bijbelscholen/parttime-bijbelscholen
How is this possible?
I have tried to setup dynamic routing in nextjs which is working fine, but I have a problem.
If I want dynamic routes which are all nested I need to do something like this:
I know it's ugly, I'm sorry.
so now when I go to
www.ourwebsite.com/onderwijs/bijbelscholen/parttime-bijbelscholen
I just get the latest word from the URL and take that and put that into my API to get the right data which is working fine! I configured strapi so it finds by slug and not by id. So my API URL looks like this: www.myStrapiInstalation/api/pages/parttime-bijbelschool
but when I go to
www.ourwebsite.com/onderwijs/parttime-bijbelscholen
It's also working! but that's not good! Because it needed to give me a 404 page because it doesn't exist but now it just takes the latest word and gets the data from strapi. My API URL is still this of course: www.myStrapiInstalation/api/pages/parttime-bijbelschool
So what do I need to do?
Is it just not possible to make everything hardcoded and do I need to make nested folders with real names like: 'onderwijs', 'activiteiten' etc.
Or can I make everything dynamic so that people can make nested routes in nested routes in strapi?
I feel like this is a very stupid question, but I'm really stuck here.
Next JS allows catch-all routes, which is useful for nesting pages and have this format:
pages/post/[...slug].js
In your example, the pages folder structure would be:
pages/secondNest/[...page].js
However, since you're using Strapi to fetch the content, the routes of these pages should be known in advance so that if the user navigates to an invalid page, the result should be 404.
You can set the valid routes in Next with getStaticPaths. But notice that you will also need to set up a custom controller in your Strapi application to return the content tree of your website the way that getStaticPaths expects.
That way, it's ok to use the last part of the URL to get the content.
One disavantage of using catch-all routes, though, is that you must know the base of each URL. For example, if you have the following URLs
www.example.com/onderwijs/bijbelscholen/parttime-bijbelscholen
www.example.com/activiteiten/bijbelscholen/parttime-bijbelscholen
the folder structure in pages would be:
pages/onderwijs/[...page].js
pages/activiteiten/[...page].js
The key factor here is that the paths following the base of the URL should be set statically with the help of a custom controller that returns the tree of the content.

Redirect to dynamic route with regex on NextJS config file

My problem is that I'm building a new site, and in my old one there was some pages with a child route /feed.
For example: www.mypage.com/blogs/blog-1/feed
So, what I'm trying to do is to redirect from /blogs/blog-1/feed to /blogs/blog-1, but now it shows me an 404 error.
These blog pages are dynamic, they are inside a [slug].tsx file, so what I've tried was to made an [...slug].tsx file, but because I'm using getStaticPaths and getStaticProps methods, it doesn't works (Or I don't know how to do it properly).
So, my other option, and the one that I think is the most appropriate, it's to add some kind of redirect regex expression on next.config.js:
{
source: '/blogs/:slug(/\feed/)',
destination: '/blogs/:slug',
permanent: true,
},
Am I right, or is there a better way to do this? Because obviously the code I have put in does not work, I am not telling to the config file how to remove that '/feed' fragment from the :slug.
Thanks in advance :)

How do I route a static page in sylius

I am having tourble creating and routing static pages from within the Sylius CMS.
I have had this working once but now just can not get it to work at all. Creating the content for the page works fine but when I try to save a route all I get is errors.
An exception has been thrown during the rendering of a template
("Parameter "id" for route "sylius_backend_static_content_update" must
match ".+" ("" given) to generate a corresponding URL.") in
SyliusWebBundle:Backend/Content/StaticContent:update.html.twig at line
20.
Image of the page I am trying to save
Any help would be appreciated as there doesn't seem to be any documentation on this part of sylius.
May be you can have a look into the link below. I also had some problems with the routes while creating the pages. I had solved it by adding some info into the database. Hope this link helps you.
https://github.com/Sylius/Sylius-Standard/issues/78
Happy Syliusing ^^

Asp.Net MVC 4 Default Url - missing trailing slash messes up href links

My project is configured to use the project url a.k.a "localhost/AppName/" and no specific page or anything, everything works fine, but if the user removes the last slash, my hrefs stop working because it ignores the "AppName", so for an example, if I've got an action called Books/Index, if the user removes the last slash "localhost/AppName" and hit enter and then clicks on the link to navigate to the books/index, the full path will be "localhost/Books/Index" which will not work, because it is missing the "AppName" after localhost.
I've tried to change the route and create a rewrite rule but still, no luck.. I'm not sure why this happends and that is what worries me, I've considered changing the way the links are generated, but I think enforcing the "/" would be better, any thoughts on this will be appreciated.
I'm using the default route by the way.
There are some ways trying to fix this, you can google it, quite some solutions there you can try. but none of them perfect.
In my option, the real fix is just to void using the relative path. In your case, you should not use books/index as the href, instead, you should always use the url helper to create the absolute path.
The cause of this problem comes from the asp.net route mechanism, so the root fix is just staying with the route mechanism.
Actually i had this problem too.. i'm not sure if this is the right solution, but this worked fine for me ! Use #Url.Action() for href..
<a href = #Url.Action("ActionName","ControllerName")>

IIS 7.0 Url Rewrite2 Rule and Regex Issues

I am using IIS.net's Url Rewrite program on my server to make rules for URL Rewrite. I have pages on my Asp.NET 4.0 Web Site such as Page.aspx, and News.aspx.
When I create a rule for Page.aspx (http://www.foo.com/Page.aspx?PageTitle=Foo to http://www.foo.com/Foo) It works great.
When I create a rule for News.aspx (http://www.foo.com/News.aspx?NewsTitle=Foo to http://www.foo.com/Foo) It works great too.
But when I create rules for both News.aspx and Page.aspx, it fails. Program looks rule order. If News's rule is at top, News.aspx Url rewritten, and Page.aspx Url rewrite fails.
If Page's rule is at top, Page.aspx Url rewritten, and News.aspx Url rewrite fails.
I guess thhis problem occurs because of regex matching. Let me show you:
Rule for News.aspx is at top. So it works fine. But when I click Page.aspx, the RewriteUserFriendlyURL1's Pattern catch my URL, and tries to redirect it News.aspx (Shown in Action Url).
I hope my description is clear.
My question is: How can I avoid this problem? For example I can make a new page called Redirect.aspx, and can redirect all pages to here. I can make 1 rule for Redirect.aspx.
But I think there should be another way to solve this issue with configuring these rules.
Thank you.
The problem is that the regular expressions for RewriteUserFriendlyURL1 and RewriteUserFriendlyURL2 are exactly the same so in that case the order in which they are listed decides which rule gets applied first.
I understand what you are trying to accomplish but it can not work like that. It's impossible for the URL rewrite module to know if /Foo is a news article or a page. Your best option is probably to put the news under /news/<title> and keep /<title> for your regular pages. In that case you can use /news/ to make a difference between news and pages.
So you should keep RedirectUserFriendlyURL2 and RewriteUserFriendlyURL2 the same but change the action URL for RedirectUserFriendlyURL1 to /news/{C:1} and change the pattern for the RewriteUserFriendlyURL1 rule to ^news/([^/]+)/?$ (note: no slash between ^ and news).

Resources