strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
63.86k stars 8.13k forks source link

[V5] can't use `useContentManagerContext` on custom field #21933

Closed dzakki closed 4 weeks ago

dzakki commented 4 weeks ago

Bug report

Required System information

Describe the bug

i was using useContentManagerContext from helper-plugin on Strapi 4 before, and it's working well. but after migrating to v5 from '@strapi/strapi/admin' it doesn't work well. it returned an error: useDocumentLayout must be used within StrapiApp``.

this error only appear when running in the prod mode (npm run build & npm run start)

Steps to reproduce the behavior

  1. create a custom field
  2. import useContentManagerContext
  3. build the strapi npm run build
  4. run the strapi npm run start

Expected behavior

it's should be working well without error

Screenshots

Screenshot 2024-10-22 at 12 09 10

Code snippets

import {
  unstable_useContentManagerContext as useContentManagerContext,
} from '@strapi/strapi/admin';

const customField = () => {
  const ContentData = useContentManagerContext()
  ....
}

Additional context

Add any other context about the problem here.

michalkos commented 1 week ago

@dzakki why is this closed? it's still an issue with 5.3.0

dzakki commented 1 week ago

Hi @michalkos , are you using the useContentManagerContext on your custom field?

michalkos commented 1 week ago

Hi @michalkos , are you using the useContentManagerContext on your custom field?

yes

dzakki commented 6 days ago

Hi @michalkos , are you using the useContentManagerContext on your custom field?

yes

here are my solutions to solve that issue when build to production.

  1. npm install
  2. remove export strapi-admin on the plugin
    // remove this
    "./strapi-admin": {
      "source": "./admin/src/index.js",
      "import": "./dist/admin/index.mjs",
      "require": "./dist/admin/index.js",
      "default": "./dist/admin/index.js"
    },
  3. add file strapi-admin.js on the plugin
    
    'use strict';

import admin from './admin/src/index';

export default admin;

5. add file strapi-server.js on the plugin

'use strict';

module.exports = require('./dist/server');


6. cd src/plugins/my-plugin
7. npm install
8. npm run build
9. back to root dir cd ../../../
10. npx strapi ts:generate-types
11. add the plugin to workspaces root package.json, e.g `"workspaces": ["./src/plugins/my-plugin"]`
12. npm install in root directory
13. npm run build
14. npm run start
michalkos commented 6 days ago

Interesting. I tried to just add workspace and keep everything else as it was before it worked as well. Thanks!

dzakki commented 6 days ago

Interesting. I tried to just add workspace and keep everything else as it was before it worked as well. Thanks!

Great! Just fyi, if you do rebuild again and got error, perhaps you need to repeat the solutions. Mostly what I did is not commit the workspaces after build.