`content` query parameter on new file page (`/-/new/`) is ignored by the editor
<!--IssueSummary start--> <details> <summary> Everyone can contribute. [Help move this issue forward](https://handbook.gitlab.com/handbook/marketing/developer-relations/contributor-success/community-contributors-workflows/#contributor-links) while earning points, leveling up and collecting rewards. </summary> - [Close this issue](https://contributors.gitlab.com/manage-issue?action=close&projectId=278964&issueIid=594214) </details> <!--IssueSummary end--> ### Summary The `content` query parameter on the "new file" page (`/-/new/<branch>`) is accepted by the server and rendered into the page HTML, but the JavaScript editor initialization ignores it, resulting in an empty editor. The `file_name` query parameter works correctly. ### Steps to reproduce 1. Navigate to any project's new file page with a `content` query param, e.g.: `https://gitlab.com/<namespace>/<project>/-/new/main?file_name=test.txt&content=hello` 2. Observe that the filename field is correctly pre-filled with `test.txt` 3. 3. Observe that the editor content is **empty** — `hello` is not shown ### What is the current bug behavior? The editor is empty despite `content=hello` being in the URL. ### What is the expected correct behavior? The editor should be pre-filled with the value of the `content` query parameter (`hello` in this case). ### Root cause analysis The server-side HAML template correctly renders `params[:content]` into a `<pre class="editor-loading-content">` tag inside the `#editor` div: **`app/views/projects/blob/_editor.html.haml` (line 43):** ```haml .js-edit-mode-pane#editor{ data: { 'editor-loading': true, testid: 'source-editor-preview-container', ref: ref } }< %pre.editor-loading-content= params[:content] || local_assigns[:blob_data] ``` I confirmed via `fetch(window.location.href)` that the server HTML *does* contain the content inside the `<pre>` tag. However, the JavaScript in **`app/assets/javascripts/blob_edit/edit_blob.js`** never reads this `<pre>` content. In `configureMonacoEditor()`: ```javascript let blobContent = ''; if (filePath) { const { data } = await Api.getRawFile( projectId, filePath, { ref }, { responseType: 'text', transformResponse: (x) => x }, ); blobContent = String(data); } this.editor = rootEditor.createInstance({ el: editorEl, blobContent, blobPath: this.options.filePath, }); ``` On the "new file" page: - `filePath` comes from `editBlobForm.data('blobFilename')` (set in `blob_editor_paths` helper), which is `nil`/`undefined` for new files since `@blob` is nil - - Because `filePath` is falsy, the `if (filePath)` block is skipped - - `blobContent` remains `''` - - `rootEditor.createInstance()` is called with empty content - - `SourceEditor.prepareInstance(el)` calls `clearDomElement(el)`, which destroys the `<pre>` tag that held `params[:content]` So the server renders the content correctly, but the JS overwrites it with an empty string. ### Suggested fix In `edit_blob.js`, when `filePath` is not set (i.e., on the "new file" page), read the content from the `<pre class="editor-loading-content">` element before it gets cleared: ```javascript let blobContent = ''; if (filePath) { const { data } = await Api.getRawFile( projectId, filePath, { ref }, { responseType: 'text', transformResponse: (x) => x }, ); blobContent = String(data); } else { const preEl = editorEl.querySelector('.editor-loading-content'); if (preEl) { blobContent = preEl.textContent; } } ``` ### Relevant files - `app/views/projects/blob/_editor.html.haml` — server-side rendering of `params[:content]` - - `app/assets/javascripts/blob_edit/edit_blob.js` — JS editor init that ignores the pre-rendered content - - `app/assets/javascripts/blob_edit/blob_bundle.js` — entry point - - `app/controllers/projects/blob_controller.rb` — controller (`editor_variables` method) - - `app/assets/javascripts/editor/source_editor.js` — `prepareInstance` calls `clearDomElement` ### Environment - GitLab.com (SaaS) - - Also affects self-managed instances
issue