Here we have the Sanity studio of a fake online candy retailer. Now, your studio might look somewhat different to ours, but this is the one that we've modelled with pages, articles, but most importantly, let's take a look at banners.
Now, these are the banners that we're going to show to visitors as they come to the page, and it's going to depend on what country they're in. I'm in the UK, and so I want to make sure that they get to see this content here. First of all, we've got a dropdown here, which is populated by a list of countries coming from an npm package, and this is the benefit of structured content in JavaScript files, is that it allows you to make near infinitely configurable schema.
Then we've got a string field here for the title, a text field for the content, and an image field where we've pulled in a delightful picture of the UK here from an Unsplash asset source. So that's the banner we want to serve to visitors, and let's take a look at what that's going to look like. I've already visited this page before, so I don't get sent that banner, but if I was to come into a new incognito window, here I get served that banner because this is where I'm at. So how does this work? Let's jump into some code.
We're going to very quickly take a look at this. The concepts here are quite straightforward, and perhaps we'll be able to make this code available as well. But here is the banner component that we're looking at. So you can see at the very top, the get banner URL here is going to fire off to the api route. API routes are a way of running server-side logic on a Next.js website. But again, most JAMstack technologies have similar concepts around running serverless functions to modify pages. And I've built in a fallback here that during development, I can simply set the country in a query string, or otherwise, if I'm in production, go and hit that get banner API route directly.
Now, a banner component here is going to set up some state, and it's also going to track using this sticky state hook, which is all that's doing is making sure we log some information to local storage. And we're going to store the date of when I saw this banner. And it would be possible to extend this component to say, if I haven't seen this banner for, say, 24 hours, it might get served again. So what we're going to do here when the component mounts is first fetch this banner URL. And if the status is not correct, we're going to set that the banner has been seen or if it's empty for whatever reason, we don't want to do that. But if the banner information does get returned properly, then we want to set that into the component and then set that the banner has been seen. And if we have if we don't have any data because we've seen this before or the fetch failed and return no data, then this component does nothing. However, if we do get banner data, then we've got some frame of motion and some tailwind to style that component being loaded into view there with nice animation and styling. So let's have a look at our API route. So as the request comes in to this API route, it is going to check first of all, what country information do we have? First of all, it's going to check, did we manually override this? Perhaps there'd be use cases to manually set the country here. Otherwise, we can check the headers on certain plans. On Vercel, you do get information about the country based on the IP. There are NPM packages as well that allow you to do that. And a lot of other providers will send along that information with requests, such as where the user is.
Comments