Can a Next.js website be designed exclusively for mobile devices?

In the process of creating my Next.js application, I have developed a page that is exclusively for mobile devices. My question is how can I ensure that this page is only accessible to users on mobile devices? In case someone tries to access it from a desktop, I'd like them to be redirected to the homepage or shown an error message instead.

/pages
   index.js
   mobile-only.js

Answer №1

To handle redirection based on user agent, you can parse the useragent header within the getServerSideProps function.

Check out the following RegEx patterns that can help with detecting mobile browsers: Detecting a mobile browser

Below is an example of how you can implement the getServerSideProps function:



export const getServerSideProps = async ({ req }) => {
  // You must define the implementation of `parseUA` yourself 
  const isMobile = parseUA(req.headers['user-agent'])

  if (!isMobile) {
    return {
      redirect: {
        destination: '/some/other/page',
        permanent: false
      }
    }
  }

  // Additional logic for handling mobile page 

  return {
    props: {
      data: {
        // ...
      }
    },
  };
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the layout of the date selector to display weekdays instead - material ui

How can I change the datepicker format to display weekdays (Monday, Tuesday,..) instead of MM/dd/yyyy? Currently, the datepicker is set up with the format format="MM/dd/yyyy". Would it be possible to modify this? Here is the source code: <MuiPickers ...

Heroku Environment causing SocketIO malfunction while it works perfectly in Localhost

While working on a chat program using NodeJS and SocketIO, I faced an issue when deploying it to Heroku Server. It turns out that SocketIO was not functioning properly in the Heroku environment. Upon checking the logs in Heroku, I found no relevant inform ...

Unable to access style property, encountering error on separate page

Once again, I'm feeling a bit lost on where to go with this coding issue. It seems quite basic, but I'm struggling to pinpoint the problem. My goal is to hide several IDs, and while it does work, I keep encountering an error: Uncaught TypeError: ...

Transforming BufferGeometry to Geometry using FBXLoader within the Three.js library

Check out my snippet below for loading a .fbx object. It defaults to loading an object as BufferGeometry: const loader = new THREE.FBXLoader(); async function loadFiles(scene, props) { const { files, path, childName, fn } = props; if (index > fi ...

GAS: What strategies can I implement to optimize the speed of this script?

I have a sheet with multiple rows connected by "";" and I want to expand the strings while preserving the table IDs. ID Column X: Joined Rows 01 a;bcdfh;345;xyw... 02 aqwx;tyuio;345;xyw... 03 wxcv;gth;2364;x89... function expand_j ...

AngularJS, the element being referenced by the directive is empty

Currently, I am in the process of transferring a jQuery plugin to AngularJS simply for the enjoyment of it. Previously, when working with jQuery, my focus was on manipulating the DOM using jQuery functions within the plugin loading function. Now that I am ...

Prisma : what is the best way to retrieve all elements that correspond to a list of IDs?

I'm currently implementing Prisma with NextJs. Within my API, I am sending a list of numbers that represent the ID's of objects in the database. For example, if I receive the list [1, 2, 12], I want to retrieve the objects where the ID is eithe ...

Prisma: Incorrectly identifying existing items where the list contains any of the specified items

Within my Prisma model, I have a property designated to store a list of phone numbers in the format phones String[] @unique When making an API call with a model that may include one or more phone numbers, my goal is to locate any existing record where any ...

What is the most effective way to organize and display objects, such as using an "interest" attribute that could be the same for multiple "User" objects?

Hey everyone, I'm facing some challenges while trying to create a function. In the code snippet provided, I aim to develop the interestMatch function. The goal of this function is to analyze all users and identify those who share the same interest - s ...

Issues with displaying HTML5 audio player in iOS Chrome and Safari browsers

My html5/jquery/php audio player is working well on desktop browsers, but when I tried testing it on iOS, all I could see was a grey track bar. I suspect that the controls are hidden behind the track bar because sometimes the associated file starts playing ...

Developing Java-based Ajax scripts

Is there a way to implement AJAX functionality in Java without actually using AJAX itself? I'm searching for a JAVA API that can achieve this. Just like we can submit data from a web page using a JAVA program, I want to handle AJAX operations through ...

Unable to modify font selection in NextUI for NextJS

I am currently utilizing Nextjs 14 with NextUi for the UI library. The app was set up using: npx create-next-app -e https://github.com/nextui-org/next-app-template However, despite my efforts to modify the fonts in font.ts, the displayed font remains unch ...

Creating a function that uses setInterval to continuously update the input with a specific value

I am looking to use the setInterval function to continuously update the value of #test1. Additionally, I want the value of #test1 to be cleared and reset to 1 second after the user clicks a button. Example output can be found here: http://jsfiddle.net/eK ...

How can I utilize target.event to close the dropdown in Vuejs when clicked outside of it?

const app = new Vue({ el: "#app", name: 'aselect', data: { value: 'Select a Fruit', list: ["Orange", "Apple", "Kiwi", "Lemon", "Pineapple"], visible: false }, methods: { toggle() { this.visible = !this.vi ...

Combining First and Last Names for MongoDB Searches

Within my MongoDB database, I have stored two fields - FirstName and LastName. On the frontend, I am receiving a string that contains both the first name and last name separated by a space. I need a query to search for a match using both the first name and ...

Is it possible to create a central hub for all routes in a large AngularJS application?

Developing a large angularjs application with the intention of utilizing require.js for lazy-loading additional modules. The main question at hand is whether to create a comprehensive route.js file containing all the routes to other modules, or if each mod ...

The response is dispatched without delay and does not necessitate awaiting

I am facing an issue with waiting for the completion of the getAllData function before proceeding further. let _partnerToken; async function getAllData(dealerId,id){ let partnerToken; var options = { 'method': 'GET', ' ...

Navigating through DynamicRoute management

I am encountering an issue with next/link when using the [folder] in next.js. view image description here Above is my pages structure. A user should be directed to the route .types/[type], where "type" is the dynamic name of the product selected by the us ...

Creating a link that triggers a submit event

I am stuck with this piece of code: <form name="ProjectButtonBar_deleteProject_LF_3" action="" method="post"> <a class="buttontext" href="javascript:document.ProjectButtonBar_deleteProject_LF_3.submit()">...</a> Instead of directly subm ...

Identifying the beginning and end of a synchronized POST request

I'm troubleshooting a simple HTML form that sends a POST request to a PHP file: <form method="POST" action="parse.php"> The parse.php file creates a downloadable file and sends it to the output buffer with specific headers: header("Cache-Cont ...