keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Cannot customize index.js

Open rankun203 opened this issue 2 years ago • 7 comments

How to reproduce

  1. add a new customized page under admin/pages/index.js
  2. run Keystone

Expected: Showing my customized page content Actual:

./pages/index.js
Error: 
  × Expected ';', got '__internal'
   ╭─[/app/.keystone/admin/pages/index.js:1:1]
 1 │ export { default } from "../../../admin/pages/index.js"__internal-do-not-use-will-break-in-patch/admin-ui/pages/HomePage';
   ·                                                        ──────────
   ╰────

Caused by:
    Syntax Error
image

Environment:

  • macOS 13.3.1
  • Node.js 16.20.0
  • @keystone-6/core 5.2.0

rankun203 avatar Jun 12 '23 10:06 rankun203

I think it's the way writeAdminFile is written can have concurrency issues.

Keystone uses Promise.all to call writeAdminFile() to write all files https://github.com/keystonejs/keystone/blob/main/packages/core/src/admin-ui/system/generateAdminUI.ts#L125.

When there are 2 files of the same path, each get a piece written to the file, result in a dubious file.

rankun203 avatar Jun 12 '23 10:06 rankun203

A temporary solution would be to write the pages/index.js using getAdditionalFiles:

import { config } from '@keystone-6/core';

config({
  ...other configs
  getAdditionalFiles: [async () => {
    return [
      {
        mode: 'write',
        src: 'export { default } from "../../../admin/pages/index.js";',
        outputPath: 'pages/index.js',
      },
    ]
  }]
})

Keystone will then skip writing it to avoid conflicting with user defined writes.

rankun203 avatar Jun 13 '23 10:06 rankun203

What is the preferred handling here - it seems like it's a semi significant decision as it forces a stronger opinion on the extensibility of custom admin pages.

I would assume that we would want to prioritise the auto-generated files to ensure core functionality.

At the moment, if I have a Todo list, and I add a custom admin/pages/todos/index.js, it'll cause the same error. With the getAdditionalFiles approach, my version will overwrite the list-generated page.

Both approaches stop core functionality from working, without being immediately clear why.

kennedybaird avatar Aug 07 '24 20:08 kennedybaird