trieb-work/strapi-provider-cloudflare-r2

Cannot compile example

Closed this issue · 6 comments

Using the example code for middlewares.js ends up with the following error:

Error: Could not load js config file /opt/app/config/middlewares.js: env is not defined
    at loadJsFile (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/load-config-file.js:18:11)
    at loadFile (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/load-config-file.js:35:14)
    at /opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/config-loader.js:18:18
    at Array.reduce (<anonymous>)
    at module.exports (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/config-loader.js:15:6)
    at module.exports (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/index.js:55:38)
    at new Strapi (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:80:23)
    at module.exports (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:557:18)
    at module.exports (/opt/app/node_modules/@strapi/strapi/lib/commands/builders/admin.js:14:26)
    at module.exports (/opt/app/node_modules/@strapi/strapi/lib/commands/build.js:12:9)

What is the proper way to define and use env() in middlewares.js file?

Hey, just had this same issue. After a little trial and error I found this to work:
process.env.CF_PUBLIC_ACCESS_URL.replace(/^https?:\/\//, ""),

@MartinMikita did you follow the docs and use the Security Middleware Configuration as described? Does the error still persist?

Yes, this is my example according to your docs:

module.exports = [
  'strapi::errors',
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": [
            "'self'",
            "data:",
            "blob:",
            env("R2_PUBLIC_ACCESS_URL", "").replace(/^https?:\/\//, ""),
          ],
          "media-src": [
            "'self'",
            "data:",
            "blob:",
            env("R2_PUBLIC_ACCESS_URL", "").replace(/^https?:\/\//, ""),
          ],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

Error message: Could not load js config file /opt/app/config/middlewares.js: env is not defined, which means, that your suggestion to use env() is not working with the example

Sorry! Can you please try it like this?:

module.exports = ({ env }) => ([
  // ...
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": [
            "'self'",
            "data:",
            "blob:",
            env("CF_PUBLIC_ACCESS_URL").replace(/^https?:\/\//, ""),
          ],
          "media-src": [
            "'self'",
            "data:",
            "blob:",
            env("CF_PUBLIC_ACCESS_URL").replace(/^https?:\/\//, ""),
          ],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  // ...
]);

So for your example:

module.exports = ({ env }) => ([
  'strapi::errors',
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": [
            "'self'",
            "data:",
            "blob:",
            env("R2_PUBLIC_ACCESS_URL", "").replace(/^https?:\/\//, ""),
          ],
          "media-src": [
            "'self'",
            "data:",
            "blob:",
            env("R2_PUBLIC_ACCESS_URL", "").replace(/^https?:\/\//, ""),
          ],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
]);

Great, that works :) Didn't know it could be possible just like that...

great to hear! I fixed the example. Thank you for pointing that out..