Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Crear una Progressive Web App (PWA) usando React y Next.js

Tiempo de lectura: 2 minutos

Today we are going to learn how we can create a Progressive Web App (PWA) using React and Next.js.

We install the dependency we are going to use next-pwa

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install next-pwa --save-dev @types/next-pwa --save-dev
npm install next-pwa --save-dev @types/next-pwa --save-dev
npm install next-pwa --save-dev @types/next-pwa --save-dev

Now we create a manifest.json file inside public.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"short_name": "My PWA",
"name": "My PWA",
"icons": [
{
"src": "/icon.png",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "/",
"display": "standalone",
"theme_color": "#ffffff",
"background_color": "#ffffff"
}
{ "short_name": "My PWA", "name": "My PWA", "icons": [ { "src": "/icon.png", "sizes": "192x192", "type": "image/png" } ], "start_url": "/", "display": "standalone", "theme_color": "#ffffff", "background_color": "#ffffff" }
{
  "short_name": "My PWA",
  "name": "My PWA",
  "icons": [
    {
      "src": "/icon.png",
      "sizes": "192x192",
      "type": "image/png"
    }
  ],
  "start_url": "/",
  "display": "standalone",
  "theme_color": "#ffffff",
  "background_color": "#ffffff"
}

We put the name and data of our app and link the icon. We can choose the orientation by indicating:   «orientation»: «portrait»

We create a file named service-worker.js inside public:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('cache01').then(function(cache) {
return cache.addAll([
'/_next/static/', // Here you should add the routes to the static files generated by Next.js
]);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response fetch(event.request);
})
);
});
var cacheWhitelist = ['cache01'];
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
});
caches.keys().then(function(cacheKeys) {
console.log('SW Version: '+cacheKeys);
});
self.addEventListener('install', function(event) { event.waitUntil( caches.open('cache01').then(function(cache) { return cache.addAll([ '/_next/static/', // Here you should add the routes to the static files generated by Next.js ]); }) ); }); self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request).then(function(response) { return response fetch(event.request); }) ); }); var cacheWhitelist = ['cache01']; caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.map(function(cacheName) { if (cacheWhitelist.indexOf(cacheName) === -1) { return caches.delete(cacheName); } }) ); }); caches.keys().then(function(cacheKeys) { console.log('SW Version: '+cacheKeys); });
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open('cache01').then(function(cache) {
      return cache.addAll([
        '/_next/static/', // Here you should add the routes to the static files generated by Next.js
      ]);
    })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(response) {
      return response  fetch(event.request);
    })
  );
});

var cacheWhitelist = ['cache01'];
caches.keys().then(function(cacheNames) {
  return Promise.all(
    cacheNames.map(function(cacheName) {
      if (cacheWhitelist.indexOf(cacheName) === -1) {
        return caches.delete(cacheName);
      }
    })
  );
});

caches.keys().then(function(cacheKeys) {
  console.log('SW Version: '+cacheKeys);
});

We add the manifest.json to our head:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link rel="manifest" href="./manifest.json" />
<link rel="manifest" href="./manifest.json" />
<link rel="manifest" href="./manifest.json" />

And now we load the web in the Chrome mobile browser to see how it prompts us to install the APP or we go to Settings and click on Install application.

0

Leave a Comment