The data in import is not being refreshed by getServerSideProps

I'm encountering an issue with getServerSideProps in NextJS where the values are not updating on page load. It seems to behave similarly to using getStaticProps when retrieving data from an imported JSON file.

Here is the relevant code snippet:

/update/[...id].js

import { hasPermission, getPermissions } from '../../lib/permissions'
...
// Using ternary operator to check user permission within the page function
{ (hasPermission(session.user.email, `${params.id[0]}-edit`, permissions)) ? <HasPerm /> : <NoPerm /> }
...
export async function getServerSideProps(context) {
  const params = context.query
  const permissions = getPermissions()
  return {
    props: { params, permissions }
  }
}

/lib/permissions.js

import permissions from '../db/users.json'

export function hasPermission(username, group, perm = false) {
  if (!perm)
    perm = permissions

  if (username in perm && perm[username].groups.includes(group)) {
    return true
  }
  if (perm.all.groups.includes(group)) {
    return true
  }
  return false
}

export function getPermissions() {
  return permissions
}

For SSR pages that were originally following this pattern, I passed permissions into the page via getStaticProps and utilized those values in my hasPermission() function to ensure revalidation upon permission changes. However, as there isn't a revalidation option in getServerSideProps, the data remains static. Previously, I used getInitialProps but it's generally discouraged due to ASO limitations.

How can I utilize getServerSideProps while ensuring revalidation of my imported JSON?

Answer №1

After deciding not to use the json import, I opted for fs instead for different reasons. The issue was ultimately resolved by implementing dynamic imports within the getServerSideProps function:

export async function getServerSideProps(context) {
  const permissions = await import('../db/users.json')
}

I also learned from my mistake of sharing the entire permissions database with all users using a function like getPermissions in getStaticProps to retrieve data for the hasPermission function on the page. Instead, it is crucial to only provide data through getServerSideProps if the user has permission. Essentially, keeping sensitive data secure on the server-side. This way, individual permissions can be passed to the page when needed, such as displaying a message indicating lack of permission.

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

Utilizing localStorage for language adjustment

To change the language on the website, we use the following code: <select> <option value = ""> </option>. The language switch is successful. <select class="tt-select" id="lang" name="forma" oncha ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

The deviceorientation event in JavaScript is not triggering on Chrome for Android

I am attempting to activate the deviceorientation event in JavaScript, but it's not firing on my Android device. If you'd like to review the event documentation, please click here and view this image: https://i.sstatic.net/HkcOQ.png window.addE ...

Can we incorporate modulo into the loop?

I have a JavaScript function with HTML code inside. I need the ".one-card" div to repeat four times within each ".row" div. The ".row" div is being repeated in a foreach loop. I want to check if the result of modulo (4) is not equal to zero, then display t ...

Searching for a nested element within an AngularJS directive

While working on a directive, I am facing an issue with using the element parameter to locate its children by class name. .directive("ngScrollList", function(){ return { restrict: 'AE', link: function($scope, element, attrs, ...

Issue with Vuex: currentUser state not persisting after page refresh

I'm currently working on a Vue.js SPA that utilizes Rails 6 API as the backend and Vue-cli (legacy webpack template). After signing in a user, everything seems to be functioning correctly. I can view the user details, it updates my setCurrentUser mut ...

Exploring the depths of web automation using Python and Selenium, harnessing the power of a while

Currently, I am navigating through various pages on iens website using Selenium and Python. My goal is to click on the "Volgende" button (which means "next" in Dutch) continuously until there are no more pages left by implementing a while loop. Specificall ...

Count up with style using the "jQuery Boilerplate" plugin for Jquery!

I am a beginner in creating jQuery plugins. The following jQuery plugin has been created using jQuery Boilerplate. It performs a count-up and notifies when the count-up is completed. I would like to have a function that restarts the count-up by setting t ...

Switch the toggle to activate or deactivate links

My attempt at coding a switch to disable and enable links using CSS is functional in terms of JavaScript, but the appearance is not changing. I am lacking experience in this area. Here is my HTML Button code: <label class="switch" isValue="0"> ...

Vertical Positioning of Tabs in Materialize CSS

My current challenge involves creating vertical tabs using materialize CSS, specifically in regards to positioning. The desired outcome is for the content to align at the same level when clicking on TAB 3. An example of the expected result can be seen in t ...

Innovative JavaScript and CSS Progression for Internet Explorer 6 with Two Layers

I am attempting to dynamically load JavaScript (and CSS) files into a webpage in the following order: index.html -> loader_a.js -> a_foo.js, a_bar.js, a_foo.css, and so on. While this process works seamlessly in Firefox using appendChild, I am encoun ...

Sorting function encounters issues in Internet Explorer 7

I've been analyzing this JavaScript code for quite some time now and I'm stuck. It's a case-insensitive sort function that handles special (Danish) characters properly, but for some reason, it doesn't work in IE7. I can't figure o ...

Issue with jQuery mouseenter() function not meeting expectations

My goal is to create a div that changes its size when hovered over (using the mouseenter and mouseleave jQuery functions) and shows another div when clicked. However, I am experiencing inconsistent behavior with my jQuery code. Here is the code I am using: ...

Limit express to only allow AJAX requests

Currently working on an Express app where I aim to restrict access to the routes exclusively through AJAX requests. Aware that this involves using the X-Requested-With header, but uncertain of how to globally block other request types. Any suggestions or ...

What is the best way to create a "check all" feature in React using child components?

I have come across a few questions here that somewhat address the issue, select all managed state of controlled checkboxes but none of them provide a solution for rendering a component with a checkbox. What I want is something like this, render: funct ...

Is there a method for decreasing the expected conv2d_Conv2D1_input from 4 dimensions to 3?

Issue: An error message states that conv2d_Conv2D1_input is supposed to have 4 dimensions, but the array provided has a shape of [475,475,3] However: The inputShape is configured as [475,475,3] Upon inspection, tensors exhibit the shape [475,475,3] Err ...

Styling JSON format in Node.js

I have a code snippet that queries a database and outputs the information in JSON format: connectionpool.getConnection(function(err, connection) { if (err) { console.error('CONNECTION error: ',err); res.statusCode = 503; ...

The fetch API in Javascript encounters issues when employed within an EJS file

I'm attempting to retrieve a file named files.json from the main directory of my locally hosted NodeJS backend and then display its contents in the console. <script> fetch("./files.json") .then(res => { return res.json() ...

Incorporate a for loop in Javascript to add a variety of items (object1, object2, and object3) to an array

Currently, I am facing a challenge in my project where I need to fetch data from an open-source API. The issue lies in the fact that the API provides objects that must be grouped into an array named ingredients. These objects are labeled as strIngredients1 ...

Remove a property from an object using Object.assign()

Is there a way to remove a key using Object.assign() rather than setting the value to undefined in this scenario? In my code, I am utilizing .filter() to iterate through certain items. When a match is found, I aim to modify the x-property value while also ...