Replies: 9 comments 10 replies
-
There is currently no mechanism to statically fetch site wide data from an API in You could use |
Beta Was this translation helpful? Give feedback.
-
It loooks like my case. Generally i pull product data from contentful. But there is part of data that are fast changed (price,availability) and i get it from external API. Right now i first push them to contentful and then pull again for nextjs. |
Beta Was this translation helpful? Give feedback.
-
Maybe you can use runtime configuration for that: https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration |
Beta Was this translation helpful? Give feedback.
-
having the same issue here. one solution might be:
|
Beta Was this translation helpful? Give feedback.
-
We can use Symbol.for and the Node export class GlobalRef<T> {
private readonly sym: symbol;
constructor(uniqueName: string) {
this.sym = Symbol.for(uniqueName);
}
get value() {
return (global as any)[this.sym] as T | undefined;
}
set value(value: T) {
(global as any)[this.sym] = value;
}
} Then I can use it like: const databaseConn = new GlobalRef('myapp.database');
if (!databaseConn.value) {
databaseConn.value = setupDatabaseConnection();
}
export const database = databaseConn.value; You can use this for plain JS too, just remove the type annotations :) |
Beta Was this translation helpful? Give feedback.
-
I need to accomplish this exact same thing. There has to be a way, right? Like @denny7 mentioned, at build time only, I need to hit a couple of APIs, mutate the returned object a bit, cache that mutated object, and then access that object from anywhere in my app. The data (on the server/api side) will not change frequently. If/when it does, I will trigger another build cycle. It's essentially the same functionality as getStaticProps, but for a globally-accessible variable/object. Anyone else have any other creative solutions? Seems like this would be a pretty handy thing to have in cases where your data won't change to much and there is no need to continually re-fetch that sitewide data. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I tried using inversify and following this example I managed to achieve a global database instance (at least it seems to work, but it's not field tested): import { Container, injectable } from 'inversify';
import sdk from 'node-appwrite';
import { client } from '@/database/appwrite';
import 'reflect-metadata';
export abstract class AbstractDatabaseClient {
abstract getClient(): sdk.Databases;
}
@injectable()
export class DatabaseClient implements AbstractDatabaseClient {
private databases: sdk.Databases;
constructor() {
client
.setEndpoint('https://cloud.appwrite.io/v1')
// ... further initialization
this.databases = new sdk.Databases(client);
}
getClient(): sdk.Databases {
return this.databases;
}
}
const container = new Container();
if (typeof window === 'undefined') {
container
.bind<AbstractDatabaseClient>(AbstractDatabaseClient)
.to(DatabaseClient);
}
// Usage:
container.get(AbstractDatabaseClient).getClient(); |
Beta Was this translation helpful? Give feedback.
-
Hello, I recently migrated my web app to Vercel from Railway and developed a working solution, which can be found here: https://rmrf-css.vercel.app, for the topic of this discussion.
/* store.js */
let fs = require('fs'),
uuid = (a) => (a ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) : ([1e7] -1e3 -4e3 -8e3 -1e11).replace(/[018]/g, uuid)),
/* may be made asynchronous */
rm = id=>fs.unlinkSync(path.join(dir, id '.txt')),
dir = '/tmp/',
path = require('path');
/*writes/reads data as text/plain, open to modifications*/
module.exports = {
read: id =>new Promise((resolve, reject)=>fs.readFile(dir id '.txt', (err, buffer)=>{
if(err) reject(err);
else resolve(buffer.toString())
})
),
write: function(data, cb, id) {
fs.writeFile(dir (id=uuid()) '.txt', data, _=>cb(id))
},
rm
} Here is how I use it and how it may be used in the wild let store = require('./store');
module.exports = async function(request, response, uuid) {
let {url, id} = request.query;
if(uuid = await store.read(id)) {
/* exists */
response.send(/* data */),
/* you may remove the file or leave it since the contents of the /tmp/ directory are likely temporary */
store.rm(uuid)
} else {
store.write('data', function(id) {
/* send the unique id for the stored data in some way as JSON or text */
response.send(id)
})
}
} I hope it serves as a viable workaround to the issue with using global variables in Vercel serverless backends |
Beta Was this translation helpful? Give feedback.
-
Hello! I would like to create a variable in server.js and use it everywhere in application. I need to make a call to an API and get a list (with categories) which I will use in my application. The list will not be changed so I need to make a call to the API only once and store it somewhere. I would like to make something like getStaticProps does but for the whole app (not only for one page). As I understood I can't use getStaticProps in _app.js. How can I achive that? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions