How to build the SSR web application and mobile apps from the same code base

Server-side rendering improves initial page load speed. The browser receives pre-rendered HTML from the server, so it takes less time to build the page.

Also, SSR sites play better with SEO. Search engine crawlers grab the site’s HTML content without …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Yevhen Bondar

Server-side rendering improves initial page load speed. The browser receives pre-rendered HTML from the server, so it takes less time to build the page.

Also, SSR sites play better with SEO. Search engine crawlers grab the site's HTML content without executing JS code. Without SSR, search engines will not index page content and will not find internal links.

NuxtJS has a Server-side rendering feature:

Server-side rendering (SSR), is the ability of an application to contribute by displaying the web-page on the server instead of rendering it in the browser. Server-side sends a fully rendered page to the client.

But you cannot use SSR when you build a static site for the mobile application. Here I'll show how to create an SSR web and cross-platform mobile application from the same code base using CapacitorJS and NuxtJS.

Building the app

As an example, we will build a simple app to show local time in Kyiv.

To test SSR, we need to get data from the API server. We will receive data from worldtimeapi.org API using the useFetch method.

Let's start with the application from the previous guide.

Remove app.vue file and add pages/index.vue with the following content:

<template>
  <div>
    <h1>🇺🇦 Kyiv Time</h1>

    <p>
      đź•Ą Current time: <b>{{ timeData.datetime }}</b>
      <a @click="$nuxt.refresh()" href="">🔄</a>
    </p>
  </div>
</template>

<script setup>
const { data: timeData } = await useFetch('https://worldtimeapi.org/api/timezone/Europe/Kiev')

</script>

Here we have:

  • Retrieving current time in const timeData variable.
  • đź•Ą displaying time on the page.
  • 🔄 refresh button.

Run yarn dev and check http://localhost:3000/ what we have:

Let's look at the page source code:

In NuxtJS, Server-side rendering is enabled by default. But what will we get if we create a static site for the mobile build? Run yarn generate && yarn preview and recheck http://localhost:3000/.

If you try to refresh the page, you will see that time doesn't change. NuxtJS retrieved time from the server during static site generation and saved it in the pre-rendered HTML page.

Let's add ssr: false to the nuxt.config.ts

import { defineNuxtConfig } from 'nuxt'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
    ssr: false,
})

and rebuild the site with yarn generate && yarn preview.

Now NuxtJS runs HTTP requests on the client side. There is no datetime in the page sources, and datetime changes every time you refresh a page.

So, we must build a web application with SSR and a mobile application in client-only mode. Let's use an environment flag MOBILE_BUILD: we will build a web application with MOBILE_BUILD=0 and a mobile application with MOBILE_BUILD=1.

You can use a .env file to store environment variables, and I'll go with export MOBILE_BUILD=1.

Change nuxt.config.ts:

import { defineNuxtConfig } from 'nuxt'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
    ssr: !process.env.MOBILE_BUILD,
})

Run yarn generate && yarn preview and verify that the application works in client-only mode.

Finally, let's build and check the Android application:

yarn cap sync
yarn cap open android

Run the application on an emulator and check that the refresh button works.

🎉 Congratulations, it works (on my laptop, at least 🙂).


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Yevhen Bondar


Print Share Comment Cite Upload Translate Updates
APA

Yevhen Bondar | Sciencx (2022-09-25T10:03:17+00:00) How to build the SSR web application and mobile apps from the same code base. Retrieved from https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/

MLA
" » How to build the SSR web application and mobile apps from the same code base." Yevhen Bondar | Sciencx - Sunday September 25, 2022, https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/
HARVARD
Yevhen Bondar | Sciencx Sunday September 25, 2022 » How to build the SSR web application and mobile apps from the same code base., viewed ,<https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/>
VANCOUVER
Yevhen Bondar | Sciencx - » How to build the SSR web application and mobile apps from the same code base. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/
CHICAGO
" » How to build the SSR web application and mobile apps from the same code base." Yevhen Bondar | Sciencx - Accessed . https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/
IEEE
" » How to build the SSR web application and mobile apps from the same code base." Yevhen Bondar | Sciencx [Online]. Available: https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/. [Accessed: ]
rf:citation
» How to build the SSR web application and mobile apps from the same code base | Yevhen Bondar | Sciencx | https://www.scien.cx/2022/09/25/how-to-build-the-ssr-web-application-and-mobile-apps-from-the-same-code-base/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.