Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(server): make vue-server-renderer not a dependency (#801)
Browse files Browse the repository at this point in the history
* fix(server): make vue-server-renderer not a dependency

this is done by making this dependency-injected to prevent the dependency being required for everyone

* add error

* use require instead

* chore: try/catch
  • Loading branch information
Haroenv authored Jun 12, 2020
1 parent 62b2645 commit 1acc3df
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"algoliasearch": "^4.0.1",
"cross-env": "^5.2.0",
"nuxt": "^2.4.5",
"vue-instantsearch": "file:../../"
"vue-instantsearch": "file:../../",
"vue-server-renderer": "2.6.11"
},
"devDependencies": {}
}
2 changes: 1 addition & 1 deletion examples/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"vue": "^2.6.7",
"vue-instantsearch": "file:../../",
"vue-router": "^3.0.2",
"vue-server-renderer": "^2.6.7"
"vue-server-renderer": "^2.6.11"
},
"devDependencies": {
"@akryum/vue-cli-plugin-ssr": "^0.5.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/ssr/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export async function createApp({
const url = context
? context.url
: typeof window.location === 'object'
? window.location.href
: '';
? window.location.href
: '';
const search = url.slice(url.indexOf('?'));

return qs.parse(search, {
Expand Down
4 changes: 2 additions & 2 deletions src/util/__tests__/createServerRootMixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('createServerRootMixin', () => {
],
})
).toThrowErrorMatchingInlineSnapshot(
`"createServerRootMixin requires the \`searchClient\` and \`indexName\` arguments to be passed"`
`"createServerRootMixin requires \`searchClient\` and \`indexName\` in the first argument"`
);
});

Expand All @@ -69,7 +69,7 @@ describe('createServerRootMixin', () => {
],
})
).toThrowErrorMatchingInlineSnapshot(
`"createServerRootMixin requires the \`searchClient\` and \`indexName\` arguments to be passed"`
`"createServerRootMixin requires \`searchClient\` and \`indexName\` in the first argument"`
);
});

Expand Down
18 changes: 14 additions & 4 deletions src/util/createServerRootMixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Vue from 'vue';
import _renderToString from 'vue-server-renderer/basic';
import instantsearch from 'instantsearch.js/es';
import algoliaHelper from 'algoliasearch-helper';
const { SearchResults, SearchParameters } = algoliaHelper;
Expand All @@ -15,7 +14,7 @@ function walkIndex(indexWidget, visit) {
});
}

function renderToString(app) {
function renderToString(app, _renderToString) {
return new Promise((resolve, reject) =>
_renderToString(app, (err, res) => {
if (err) reject(err);
Expand Down Expand Up @@ -55,7 +54,18 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) {
* @returns {Promise} result of the search, to save for .hydrate
*/
search.findResultsState = function(componentInstance) {
let _renderToString;
try {
_renderToString = require('vue-server-renderer/basic');
} catch (e) {
// error is handled by regular if, in case it's `undefined`
}
if (!_renderToString) {
throw new Error('you need to install vue-server-renderer');
}

let app;

return Promise.resolve()
.then(() => {
const options = {
Expand All @@ -82,7 +92,7 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) {
uiState: app.instantsearch._initialUiState,
});
})
.then(() => renderToString(app))
.then(() => renderToString(app, _renderToString))
.then(() => searchOnlyWithDerivedHelpers(helper))
.then(() => {
const results = {};
Expand Down Expand Up @@ -229,7 +239,7 @@ export function createServerRootMixin(instantSearchOptions = {}) {

if (!searchClient || !indexName) {
throw new Error(
'createServerRootMixin requires the `searchClient` and `indexName` arguments to be passed'
'createServerRootMixin requires `searchClient` and `indexName` in the first argument'
);
}

Expand Down

0 comments on commit 1acc3df

Please sign in to comment.