Docs
Advanced
Server Data

Server Data

A feature has been added to execute functions that take a long time to compute or degrade performance only on the first request and then send stored cache data.

You can see that after the first request, all responses are printed immediately.

Typescript
import { GET } from '@zely-js/core';
import { setTimeout } from 'timers/promises';
 
async function greeting() {
  await setTimeout(1000);
  return 'Hello, Doe!';
}
 
export default [
  GET(async (ctx) => {
    const { data } = await $store(greeting);
    ctx.send(data);
  }),
];

TIP

You don't need to import any other modules. Just use the $store function directly.

After the first request, the $greeting function will no longer be executed.

INFO

The code in line 11 will be:

Javascript
let { data: r } = yield $store(
  $greeting,
  [],
  'pages\\index.ts-586' // unique id
);
e.send(r);

#Dependencies

To handle dynamically changing dependencies, such as parameters (req.params), you can register dependencies with the store.

Typescript
$store(fn, [someDep]);

Data is separated and stored based on the registered dependency value.

#Try this feature

This feature works based on serpack.
To use this feature, enable the --serpack flag.

Terminal
zely dev --serpack