Docs
Advanced
Server Data

Server Data

WARNING

This feature is currently under development.

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.

To use this feature, you must change your function according to the following rules:

  1. The function must be an asynchronous function. (or return new Promise())
  2. The function name must start with $.

Rule 2 is a temporary solution to prevent excessive caching. It can be modified to a better rule at any time.

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

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

INFO

The code in line 11 will be:

Javascript
const t = yield $serpack_cache(
  $greeting(e.params.params),
  this.context,
  '$greeting'
);

Data is stored with the following IDs:

Typescript
const id = `${name}-${context.__DEV__.path}-${Object.values(context.params).join('-')}`;

#Try this feature

This feature is provided in serpack plugin.

Typescript
// .serpackrc.ts
 
// before all, install plugin:
// npm i --save-dev @zely-js/optimizer
import { optimizer } from '@zely-js/optimizer';
 
export default <import('serpack').Options>{
  compilerOptions: {
    plugins: [optimizer()],
  },
};

To use this feature, enable the --serpack flag.

Terminal
zely dev --serpack