Using `polymer serve` with local API server - polymer

I want to use polymer serve to serve my LitElement app during development without waiting for polymer build to finish after every change. However, the app uses relative URLs for API access, like GET /api/api_method, and AFAIK I can't make polymer serve and my server work on the same port (e.g. localhost:8080).
Currently I run polymer build and then run my local Python server, which serves the Polymer files as static.
The ideal scenario would be:
$ run_my_server.sh --port=8081
$ polymer serve --api_server="localhost:8081"
Then for routes that are found in the polymer build directory they would be served, otherwise the request would be routed to localhost:8081.
Are there any other ways to setup the local dev process without rebuilding the whole app after every change?

Usually, you would do this with a proxy middleware - however, polymer server does not allow to add your own middleware.
So you have 2 options:
basically what you are doing e.g. wrapping polymer serve and forward requests
use a different server that supports a proxy middleware
As an example the es-dev-server.
install
npm i -D es-dev-server koa-proxies
create a es-dev-server.config.js
const proxy = require('koa-proxies');
module.exports = {
port: 9000,
middlewares: [
proxy('/api', {
target: 'http://localhost:8081',
})
],
};
start with
es-dev-server --node-resolve
Now if you hit http://localhost:9000 you will be served by the es-dev-server. However, if you hit http://localhost:9000/api then it will actually serve from your api server.
Doing this allows to simply use fetch when requesting from the api as all get served from the same domain + port.
You can find more details for here: https://open-wc.org/developing/es-dev-server.html#custom-middlewares-proxy
PS: I am a co-maintainer

Related

Vue.js is detected on this page. Devtools inspection is not available because it's in production mode or explicitly disabled by the author

I'm trying to enable vue-devtools in Google Chrome. But I cannot enable it. I'm using vue.js inside the Laravel application.
My server runs using php artisan serve command.
I was seeing the error message in this question's title and this solution worked for me:
Add Vue.config.devtools = true to the file where you create the Vue instance (main.js for me).
Note that, as mentioned in this answer, you need to put the Vue.config.devtools = true line before you create your store in order for the Vuex part of the devtools to work. If you're creating your Vuex store in a separate file (e.g. store.js), you may need to have the Vue.config.devtools = true line in both your main.js file as well as the store.js file.
Below is what the changes looked like in my project:
If the page uses a production/minified build of Vue.js, devtools
inspection is disabled by default so the Vue pane won't show up.
To make it work for pages opened via file:// protocol, you need to
check "Allow access to file URLs" for this extension in Chrome's
extension management panel.
I had to restart the chrome, and it worked :-)
If your using CDN; make sure your not using a production (minified) build of the library.
Use: https://unpkg.com/vue#2.4.4/dist/vue.js
Instead of: https://unpkg.com/vue#2.4.4/dist/vue.min.js
You might need to do Ctrl+Alt+I for it to show up the first time. (Source)
Updated Aug 2022
So apparently as #kissu said, the answer below causes the released code to be an unoptimized one. This might be different than what you want if you want to check production code while being able to check Vue Dev Tools.
Just be aware of it. Unless you don't mind checking the released code in an unoptimized bundle, then the following script is fine. If you don't like the Vue.config.devtools value being static, it might be time to consider env variables or something similar.
Here's how to setup Environtment Variables in Vue
Alternative answer for Vue CLI 3.x
Besides what #NathanWailes has said, this is an alternative which allows the Dev Tools to be available through scripts instead of writing it in your main Vue entry (which is usually main.js or index.js).
You can do this by simply adding this script to package.json
scripts: {
"start:dev": "vue-cli-service build --mode=development"
}
Explanation
This was because Vue.config.devtools are set to false by default in production mode as said by this GitHub Issue. But this has a work around, simply by using --mode=development flag provided in the documentation.
Then you can run using npm run start:dev and check the file in your dist/ folder! ;)
You may use the dev version of vue.js. For example get it here: https://unpkg.com/vue#2.3.2
When using Laravel just make sure you run the proper webpack for your environment for development . Running
npm run watch
should build Vue with debug mode on. Using
npm run production
minifies Vue for production. This will save you having to remember to toggle the debug mode when building for production.
For me Installing latest Vue dev tools - link and enabling 'Allow access to file URLs' in extension settings resolved the issue.
make sure you're running a non-production build of Vue.js. https://github.com/vuejs/vue-devtools/issues/62
Just add into vue.config.js:
module.exports = {
configureWebpack: {
devtool: 'source-map'
}
}
delete package-lock.json, node_modules, run npm i and VueJS Devtool will be working
you could try to set environment variable NODE_ENV to 'development'
(e.g. set NODE_ENV=development on Windows or export NODE_ENV="development" under Linux)
before launching Vue dev server.
In my case for Laravel 9 fresh installation, I forgot to run sail npm run dev.
If you're using Vite you can configure your environment directory via shared options. If you change that and have NODE_ENV set to production you'll receive this message when trying to inspect your app.

polymer serve with components

I have been facing issues with the polymer-cli since I installed it. I am not able to serve components during development. I have to write my own Node.js server with remapping urls to work with components. But the documentation page clearly suggests that it had already done this work with its own tool.
What I did
Create a component using polymer-cli's polymer init
Serve the component using polymer serve
The result is unexpected and caused the following errors:
Polymer version: 0.18.0-pre.15
Ubuntu 16.04.2 LTS x64
On running polymer serve
info: Files in this directory are available under the following URLs
applications: http://127.0.0.1:8081
reusable components: http://127.0.0.1:8081/components/test-element/
A) If I try to open localhost:8081
GET http://localhost:8081/webcomponentsjs/webcomponents-loader.js 404 (Not Found)
GET http://localhost:8081/iron-component-page/iron-component-page.html 404 (Not Found)
B) If I try to open `localhost:8081/components/test-element/demo/
The url redirects to http://localhost:8081/home/user-name/project-root/test-element/
The index page in the project root directory is served.
GET http://localhost:8081/home/user-name/project-root/webcomponentsjs/webcomponents-loader.js
GET http://localhost:8081/home/user-name/project-root/iron-component-page/iron-component-page.html 404 (Not Found)

bootstrapped jekyll project fails to load its auto-generated css

I've created a new jekyll project precisely following the simple command sequence at https://jekyllrb.com/. However it seems like the server is not looking for the right main.css:
GET http://example.com/css/main.css 404 (Not Found)
This error in the browser console seems to imply an irrelevant domain for a local project, so I wonder whether those instructions just mentioned are not what one should do for bootstrapping and running/testing a jekyll project on your local server. In that case, what is the right procedure for working locally? and is there an automated command for setting a different host name for deploying to a real server?
I am using a fresh install of jekyll 3.2.1.
Thanks!
Dirty temporary patch (because of this issue).
In _config.yml, set url: http://matanster.github.io/bloglike or wathever is your production url.
Create a config_dev.yml and add url: http://127.0.0.1
Locally, you can serve with :
bundle exec jekyll serve --config _config.yml,_config_dev.yml

Polymer serve and service worker

I am trying to get started on debugging my Polymer application. I have hand crafted it by copying what I think the PolymerCLI Polymer init does.
I am not sure what is loading service worker. The default one for development just does a console.info() call saying its been disabled for development.
When I use Polymer serve to serve my application on localhost: 8080, I get the console.info message, despite there being nowhere where I actually load the file service-worker.js . Because the application is much more complex (and I am trying to use http/2) I have my own node based server as well. When I run that and then fetch my application in the browser, service-worker.js does not get loaded and run.
What is Polymer serve doing to enable it?
It could be that a different application which used the same source (e.g. localhost:8080) registered and installed a service worker.
Open up the Application panel in Chrome Canary to inspect / delete the service worker.
If you can't access Chrome Canary, open chrome://serviceworker-internals, find the scope that matches your app, and click Unregister. There's also an option at the top of serviceworker-internals which lets you open a DevTools window and pause JS on the SW. Enable that option and you'll be able to see which SW is running.

Google Drive Open URL development workaround

it's possible to setup a Open URL for MIME type support at (console.developer.google.com). That's create and works fine for my application. But only for the product instance at Google Cloud. In detail:
I've released my application in version 1. It's AppEngine base and it supports a Open URL myapp.appspot.com/oauth2callback. So after the release I will going on with feature development for version 2. I use the local AppEngine dev setup. So I take my second client ID and change the Open URL to my local workstation mydev.workstation.com:8080/oauth2callback. That works fine, but it breaks my product instance. Is there any workaround to use Open URL for the production instance and a second Open URL for my dev instance? In my opinion the Drive SDK settings should be:
enable Authentication Production:
Client ID: xxx
Additional Scopes:
Install URL:
Open URL: https://myapp.appspot.com/oauth2callback
enable Authentication Development:
Client ID: xxx
Additional Scopes:
Install URL:
Open URL: http://devworkstation.com:8080/oauth2callback
Jens
Create separate projects in the console for prod/qa/dev
The answer from #SteveBazyl is the preferred one for all the reasons stated. A kludgy alternative would be to have the servlet that is bound to the Open URL detect when it is dealing with a test scenario and do a 301 redirect to your dev server.
Also, the API URL is pretty consistent, so once you've sniffed it once, it's not to difficult to path the host and use it in a curl script or browser bookmark for local testing.
As an aside "oauth2callback" is a slightly odd name for your Drive Open URL. It's not wrong, but "driveopen" would show the separate concerns.