Skip to content

Commit

Permalink
馃悰 Fixed redirects.json file corruption on upload
Browse files Browse the repository at this point in the history
refs 91efa46

- Referenced commit introduced a double json-stringification to uploaded redirects.json files.
- The endpoint has no stability index of any sort and is meant to be dropped in Ghost v5. It's best to rework the redirects to the yaml format as descirbe here - https://ghost.org/docs/tutorials/implementing-redirects/#file-structure
  • Loading branch information
naz committed Nov 25, 2021
1 parent 621cfd9 commit 3315ed3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/server/services/redirects/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class CustomRedirectsAPI {
validation.validate(parsed);

if (ext === '.json') {
await fs.writeFile(this.createRedirectsFilePath('.json'), JSON.stringify(content), 'utf-8');
await fs.writeFile(this.createRedirectsFilePath('.json'), JSON.stringify(parsed), 'utf-8');
} else if (ext === '.yaml') {
await fs.copy(filePath, this.createRedirectsFilePath('.yaml'));
}
Expand Down
21 changes: 21 additions & 0 deletions test/regression/api/canary/admin/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('Redirects API', function () {

const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(2);

const fileContent = fs.readFileSync(path.join(contentFolder, 'data', 'redirects.json'), 'utf-8');
fileContent.should.eql(JSON.stringify([{
from: 'k',
to: 'l'
}]));
});
});

Expand Down Expand Up @@ -143,6 +149,12 @@ describe('Redirects API', function () {
.then(() => {
const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(3);

const fileContent = fs.readFileSync(path.join(config.get('paths:contentPath'), 'data', 'redirects.json'), 'utf-8');
fileContent.should.eql(JSON.stringify([{
from: 'e',
to: 'b'
}]));
});
});
});
Expand Down Expand Up @@ -198,6 +210,9 @@ describe('Redirects API', function () {
const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(2);

const fileContent = fs.readFileSync(path.join(config.get('paths:contentPath'), 'data', 'redirects.yaml'), 'utf-8');
fileContent.should.eql('302:\n c: d');

// Provide another redirects file in the root directory of the content test folder
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects-something.json'), JSON.stringify([{
from: 'e',
Expand Down Expand Up @@ -229,6 +244,12 @@ describe('Redirects API', function () {

const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(3);

const fileContent = fs.readFileSync(path.join(config.get('paths:contentPath'), 'data', 'redirects.json'), 'utf-8');
fileContent.should.eql(JSON.stringify([{
from: 'e',
to: 'b'
}]));
});
});
});
Expand Down

0 comments on commit 3315ed3

Please sign in to comment.