GithubHelp home page GithubHelp logo

Comments (6)

dtslvr avatar dtslvr commented on April 19, 2024 1

I'm in the same boat, @dtslvr . Were you able to get it running?

No, but I'm going with a solution @geongeorge has suggested using an express middleware. Something like this:

// main.ts

app.use(StaticMiddleware);

and

// static.middleware.ts

export const StaticMiddleware = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  if (...) {
    response.send(transformedIndexHtml);
  } else {
    next();
  } 
}

from serve-static.

didaquis avatar didaquis commented on April 19, 2024

I'm on the same position.

@Kuirak do you found a solution?

from serve-static.

Kuirak avatar Kuirak commented on April 19, 2024

I'm on the same position.

@Kuirak do you found a solution?

@didaquis We are currently using the patch above via patch-package and do some simple string replace in the resulting html

from serve-static.

dtslvr avatar dtslvr commented on April 19, 2024

I have successfully set up your patch @Kuirak with serve-static 4.0.0 and configured ServeStaticModule like this:

ServeStaticModule.forRoot({
  rootPath: join(__dirname, '..', 'client', 'en'),
  serveRoot: '/en',
  transformIndexHtml: (indexHtml: string) => {
    console.log(indexHtml);
    return indexHtml;
  }
})

ExpressLoader is registered, but renderFn (and transformIndexHtml) is never called. Any idea why?

from serve-static.

alan-rotenberg avatar alan-rotenberg commented on April 19, 2024

I'm in the same boat, @dtslvr . Were you able to get it running?

from serve-static.

alan-rotenberg avatar alan-rotenberg commented on April 19, 2024

I tinkered with it a bit - it doesn't work because renderFn isn't called after you call espress.static()

This isn't what you'd check into the repo, but this got me past the issue --

diff --git a/node_modules/@nestjs/serve-static/dist/interfaces/serve-static-options.interface.d.ts b/node_modules/@nestjs/serve-static/dist/interfaces/serve-static-options.interface.d.ts
index a996aff..e1dd974 100644
--- a/node_modules/@nestjs/serve-static/dist/interfaces/serve-static-options.interface.d.ts
+++ b/node_modules/@nestjs/serve-static/dist/interfaces/serve-static-options.interface.d.ts
@@ -5,6 +5,7 @@ export interface ServeStaticModuleOptions {
     renderPath?: string | RegExp;
     serveRoot?: string;
     exclude?: string[];
+    transformIndexHtml?: (indexHtml: string) => string;
     serveStaticOptions?: {
         cacheControl?: boolean;
         dotfiles?: string;
diff --git a/node_modules/@nestjs/serve-static/dist/loaders/express.loader.js b/node_modules/@nestjs/serve-static/dist/loaders/express.loader.js
index 75c9f9b..2411238 100644
--- a/node_modules/@nestjs/serve-static/dist/loaders/express.loader.js
+++ b/node_modules/@nestjs/serve-static/dist/loaders/express.loader.js
@@ -43,6 +43,23 @@ let ExpressLoader = class ExpressLoader extends abstract_loader_1.AbstractLoader
                 app.get(renderPath, renderFn);
             }
             else {
+                app.use((req, res, next) => {
+                    if('/' == req.originalUrl && options.transformIndexHtml != null) {
+                        fs.readFile(indexFilePath, 'utf8', (err, data) => {
+                        if (err) {
+                            next(err);
+                            return;
+                        }
+                        const transformed = options.transformIndexHtml(data);
+                        res.set('Content-Type', 'text/html');
+                        res.send(transformed);
+                        })
+                    }
+                    else{
+                        next();
+                    };
+                });
+
                 app.use(express.static(clientPath, options.serveStaticOptions));
                 app.get(options.renderPath, renderFn);
             }

from serve-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.