Skip to content

Commit

Permalink
rss: use default language when accept-language query is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ntzyz committed Mar 26, 2019
1 parent 22fe19d commit febcf41
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"dev": "node index",
"dev": "node --inspect index",
"start": "cross-env NODE_ENV=production node index",
"build": "rm -rf dist && mkdir dist && npm run build:client && npm run build:server",
"build-win32": "rmdir /Q /S dist & mkdir dist & npm run build:client & npm run build:server",
Expand Down
2 changes: 1 addition & 1 deletion plugins/rss-feed/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function installer ({ site, utils, config }) {
});

router.get('/', async (req, res) => {
const acceptLanguage = req.query.acceptLanguage || req.headers['accept-language'] || 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7,ja;q=0.6';
const acceptLanguage = req.query.acceptLanguage || '';

try {
let cursor = utils.db.conn.collection('posts').find({
Expand Down
2 changes: 1 addition & 1 deletion plugins/rss-feed/server/rss2.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rss(version="2.0", xmlns:atom="http://www.w3.org/2005/Atom", xmlns:content="http
each post, index in posts
item
title=post.title
link=link + 'post/' + post.slug
link=link + 'post/' + post.slug + '?preferLanguage=' + post.language
guid(isPermaLink="false")=post._id
category=post.category
pubDate=post.date.toUTCString()
Expand Down
4 changes: 1 addition & 3 deletions src/api/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ function fetchPostBySlug (params) {
const options = {};
if (params.preferLanguage) {
options.headers = {
'Accept-Language': params.preferLanguage,
'accept-language': params.preferLanguage,
};
}

console.log(options);

return new Promise((resolve, reject) => {
axios.get(`${config.api.url}/post/by-slug/${encodeURIComponent(params.slug)}${params.password ? `?password=${encodeURIComponent(params.password)}` : ''}`, options)
.then(response => resolve(response.data.post))
Expand Down
1 change: 0 additions & 1 deletion src/components/admin/AccessLogs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default {
this.socket = io.connect(url.origin, { path: url.pathname, query: `token=${this.$store.state.token}` });
this.socket.on('log', text => {
this.logs.push(text);
console.log('here');
setTimeout(() => {
let list = document.querySelector('div.container');
list.scrollTop = list.scrollHeight;
Expand Down
1 change: 0 additions & 1 deletion src/components/admin/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export default {
});
} else {
api.page.createPage({ token: this.$store.state.token, page: this.page }).then(data => {
console.log(data);
this.editing = data.id;
alert('创建成功');
});
Expand Down
6 changes: 2 additions & 4 deletions src/entry-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import axios from 'axios';
import regeneratorRuntime from 'regenerator-runtime';

function axiosMiddleware (config) {
config.headers = {
'server-side-rendering': 'true'
};
config.headers['server-side-rendering'] = true;

axiosMiddleware.configLang(config);

Expand All @@ -22,7 +20,7 @@ axios.interceptors.request.use(axiosMiddleware, function (error) {

export default context => new Promise((resolve, reject) => {
axiosMiddleware.configLang = function (cfg) {
if (context.acceptLanguage) {
if (context.acceptLanguage && !cfg.headers['accept-language']) {
cfg.headers['accept-language'] = context.acceptLanguage;
}
};
Expand Down
1 change: 0 additions & 1 deletion test/server/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('Testing post-related APIs.', () => {

expect(response.body.status).to.be.ok;
expect(response.body.post).not.to.be.undefined;
// console.log(response.body.post); process.exit(0);

Object.keys(postTemplate).forEach(key => {
if (key === 'date') {
Expand Down
1 change: 0 additions & 1 deletion utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async function logger (req, res, next) {
let message = `[${new Date().toLocaleString()}] ${req.headers['x-real-ip'] || req.ip} - ${req.method} ${req.url} - ${req.headers['user-agent']}`;

// Write log to stdout, and push to the log array.
// console.log(message);
logger.logs.push(message);

// Keep the log array size not to big
Expand Down
5 changes: 3 additions & 2 deletions utils/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function addSpanEachLine (html) {
}

function render (posts, options) {
const acceptLanguage = options.acceptLanguage || 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7,ja;q=0.6';
const acceptLanguage = options.acceptLanguage || '';

return posts.map(origPost => {
let post = JSON.parse(JSON.stringify(origPost));
Expand All @@ -27,7 +27,7 @@ function render (posts, options) {
let matchedBody = null;
if (availableLanguages[0].priority < 0) {
// Nobody matched, use default;
matchedBody = post.body.filter(body => body.default)[0];
matchedBody = post.body.filter(body => body.default)[0] || post.body[0];
} else {
// use language which has max priority
matchedBody = post.body.filter(body => body.language === availableLanguages[0].name)[0];
Expand Down Expand Up @@ -120,6 +120,7 @@ function render (posts, options) {

// Finally, remove original source & add title
post.title = matchedBody.title;
post.language = matchedBody.language;
delete post.body;

// *Always* delete password after rendering.
Expand Down

0 comments on commit febcf41

Please sign in to comment.