How to use coffeescript with Sinatra - sinatra

I'm trying to get coffeescript working with Sinatra. I'm new to both technologies so this is probably something silly. My problem seems to be that the coffeescript compiles to javascript but doesn't execute on page, instead appearing as html.
#sinatra app
require 'coffee-script'
get "/test.js" do
coffee :hello
end
#hello.coffee
alert "hello world"
#My page (/test.js) doesn't execute the js - just displays the code
#On screen in the browser I get this:
(function() {
alert("hello world");
}).call(this);
#In the HTML I get this within the body tags
<pre style="word-wrap: break-word; white-space: pre-wrap;">(function() {
alert('hello world!');
}).call(this);
</pre>

Hmm... it looks like your example is based on this Sinatra documentation. But for some reason, Sinatra is trying to serve the .js file as HTML, and is preprocessing it accordingly. Are you by any chance setting content_type elsewhere in your application? Try changing your code to
get "/test.js" do
content_type "text/javascript"
coffee :hello
end
You could also try a completely different approach, using either Rack::Coffee or Barista to compile your CoffeeScript to JavaScript automatically at the Rack level. That might be easier if you have a large number of CoffeeScript files anyway.
Edit: After posting the above, it struck me that I'm probably just misinterpreting your markup. Is what you see when you load the page test.js in your browser just
alert('hello world!');
? If so, everything is working fine. JavaScript is only going to run in your browser when it's in an HTML page between <script> tags, or referenced via <script src="test.js"></script>. So in addition to your existing code, add
get "/alert", :provides => 'html' do
'<script type=src="test.js"></script>'
end
then open that alert address in your browser, and the script should run.

From sinatra-coffee-script-template
I was just looking for the same setup.
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'coffee-script'
get '/' do
erb :index
end
get '/application.js' do
coffee :application
end
then in application.coffee
$(document).ready ->
$('button:first').click ->
$('body').toggleClass 'dark', 'light'
index.erb
<h1>I'm living the dream</h1>
<button>Click me</button>
layout.erb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Sinatra Coffee-Script Template</title>
<style type="text/css">
.dark {
background: #2F2F2F;
color: #7F7F7F;
}
.light {
background: #EEEEEE;
color: #2F2F2F;
}
</style>
</head>
<body>
<%= yield %>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script src="/javascripts/listeners.js" type="text/javascript"></script>
</body>
</html>

I usually just setup a watcher on my CoffeeScript files while developing coffee -wc dirname/ and then deploy the compiled JS files to production. It's not ideal, but it's less complicated in some ways and removes the dependency on Node.JS from my production server (which in my case is Heroku.)

Use a gem like sinatra-asset-snack (https://rubygems.org/gems/sinatra-asset-snack) or even better, use a bootstrap to start your project so you don't have to worry about setting up all the plumbing (https://github.com/benkitzelman/sinatra-backbone-bootstrap)

Related

What does "Unable to inject Capacitor" mean and how can I fix it?

I tried migrating a Cordova test app to Capacitor according to 1 and encountered the following error in logcat on Android:
E/Capacitor( 3537): Unable to inject Capacitor, Plugins won't work
and the deviceready event seems to be missing.
Here is www/index.html:
<html>
<body>
<h1>demo-app</h1>
<div id="status"/>
<script src="cordova.js"></script>
<script src="app.js"></script>
</body>
</html>
Here is www/app.js:
document
.getElementById('status')
.appendChild(document.createElement('b'))
.appendChild(document.createTextNode('status:'))
document.addEventListener('deviceready', function () {
document
.getElementById('status')
.appendChild(document.createElement('i'))
.appendChild(document.createTextNode('ready'))
})
FYI the Cordova test app was generated by generate-cordova-package and modified to remove the extremely simple HEADER and CSS.
[1] https://capacitor.ionicframework.com/docs/cordova/migrating-from-cordova-to-capacitor/
After a bit of research, I discovered the following gem from this comment on ionic-team/capacitor issue 1572:
Capacitor injects itself into html files searching for the head tag
So just inserting <head></head> into the HTML resolves this issue and causes the Cordova-compatible deviceready event to be sent.
I really hope to see this documented in 1 and 2 the near future.
[1] https://capacitor.ionicframework.com/docs/cordova/migrating-from-cordova-to-capacitor/
[2] https://capacitor.ionicframework.com/docs/getting-started/
P.S. This subject is also discussed here: https://forum.ionicframework.com/t/unable-to-inject-capacitor/178713
I raised this PR to get this documented: https://github.com/ionic-team/capacitor/pull/3024

v5.6.1 publish instance redirecting for certain files

I started up a clean AEM 5.6.1 publish instance and am trying to change the default admin password. I went to http://localhost:4503/libs/granite/security/content/useradmin.html, but the page does not load correctly. I'm able to tell that some files load correctly (jquery.js, utils.js), but other files (userpicker.css, userpicker.js) respond with a JavaScript redirect:
<html>
<head>
<script type="text/javascript">var u="/content/geometrixx/en/toolbar/account/login.html?resource=%2Flibs%2Fgranite%2Fui%2Fcomponents%2Ffoundation%2Fform%2Fuserpicker%2Fclientlibs%2Fuserpicker.css&$$login$$=%24%24login%24%24"; if ( window.location.hash) {u = u + window.location.hash;} document.location = u;</script>
</head>
<body>
<!-- QUICKSTART_HOMEPAGE - (string used for readyness detection, do not remove) -->
</body>
</html>
What's interesting is that although I can't access userpicker.css, I am able to navigate to userpicker/userpicker.css without any problems.
Can someone explain what's happening here, and what I need to do to correct this behavior?

SailsJS load assets from local server when environment=DEV and load assets from CDN when environment=PROD

Hi I have a web app and when I'm working on my computer and on dev mode in a rural area I need to be able to use the assets form a local folder. But when I deply my code to the PROD server I need to able to switch the assets to be loaded from a CDN.
Is this possible in anyway with the help of sails.environment global ??
for example
<% if(sails.environment == 'PROD') { %>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-cookies.min.js"></script>
<% } else { %>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/angular.min.js"></script>
<script src="/js/angular-resource.min.js"></script>
<script src="/js/angular-cookies.min.js"></script>
<% } %>
Thank you.
Those tags are inserted by Grunt.
I think you can modify tasks/config/sails-linker.js to achieve what you want :
there is a section devJS and prodJs i think you should modify them according to your requirement.
Note that i don't know if it's the good practice in sails.
Edit few more things which could help :
sails lift --> Runs the default task (tasks/register/default.js).
sails lift --prod --> Runs the prod task (tasks/register/prod.js).
out from http://sailsjs.org/#/documentation/concepts/Assets/TaskAutomation.html
You can achieve what you were going for in your example using sails.config.environment or process.env.NODE_ENV, but a better solution might be to use the environment-specific config files to specify an asset path. For example, in config/development.js, you could have:
"assets_path": "/js"
and in config/production.js:
"assets_path": "//ajax.googleapis.com/ajax/libs/jquery/1.10.2"
Then in your view:
<script src="<%=sails.config.assets_path%>/jquery.min.js"></script>

app_offline.htm file does not work

I have been battling with this for ages now but I just can not get it to work.
Every blog/site I have been too says there is nothing you need to do in IIS but this cannot be correct as there are multiple website configurations such as Application, virtual directory, simple php/asp websites, vitual.
Can someone please explain to me what the setup needs to look like in IIS7.
I have:
Checked the file spelling: app_offline.htm
Made sure the file was at least 512 bytes (saw this on a random site)
Made sure it is in fact in the root of the application/website
Checked that I can browse directly to the file
Made sure the application pool framework was set to v2.0 or v4.0
Made sure the above application pool was assigned to my website
Tried this in a new website in IIS where the app_offline.htm was the only file in the root.
I have multiple websites set up that I have tested with, namely:
MVC3 Web Application
PHP Simple Website
Classic ASP Simple Website
Webforms Website
Webforms Application
Virtual folders in the above sites
Applications within the above sites
All of the above are working, and placing the app_offline.htm does absolutely nothing.
Please can someone provide some clarity.
I recently had the same issue with the app_offline file and the real problem I had was that windows was set to hide known file extensions. So when the file app_offline.htm was created I thought that the name was correct, but windows was hiding the extension .txt.
Create a web.config file with following content
<?xml version="1.0"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Hope that helps.
I was also struggling a little bit with this issue.
Along with the fore-mentioned criteria in the other answers. It turns out that the file type MUST be specified specifically .htm NOT .html.
I had the same issue, and although I couldn't solve it, I found a reasonable workaround. I added the same file, but named "appoffline.htm" to the root directory and leave it there permanently.
When I need to take application offline, I use the IIS HTTP Redirection setting for the website to redirect all incoming requests to appoffline.htm (make sure to tick "Redirect all requests to exact destination").
So I never did find a solution to this problem but I did find an alternative to what I was trying to achieve.
Basically, I wanted to show a specific "offline" page per app which would show when the site was offline. Here is what I did...
I created a website I called "_offline" in IIS. I then added a generic "catch all" binding for Port: 80 and left the host name blank. You may need to disable your current default website before this binding will be accepted.
Create an index.html page and put whatever content in there you want to show and shove it as the default page for "_offline". I'll include a bit of script below that works pretty well.
Now you can test by turning off your website, you should see your new index page. If you can't turn off the website, add a binding in your hosts file to anything like "testdomain.com" and point that to your server. Entering that in your browser should then show your offline page.
Just bare in mind, this page will show any time your IIS can not find an active website at the address coming in. Depending on your setup, this may or may not be acceptable in which case you should not use this method.
Now for my index page. I put some javascript in to determine which site the user is trying to reach, then reveal a portion of the html. I also have a countdown that runs and tries to refresh the page every 10 seconds.
Anyway, not the ideal result, but it works.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width = device-width">
<title>Under Maintenance</title>
</head>
<style>
body{
color:#014795;
}
.container{
max-width:650px;
margin:auto;
font-size:18px;
font-family:Arial, Helvetica, sans-serif;
padding:20pt;
text-align:center
}
#logo img {
max-width:100%;
}
a {
color: inherit;
text-decoration: none;
}
a:hover {
color: inherit;
text-decoration: none;
}
</style>
<body>
<table class="container">
<tr>
<td>
<span id="logo"></span>
<p>This site is currently under maintenance and will be available shortly.</p>
<p>We apologize for any inconvenience caused.</p>
<table style="text-align:left;margin:auto;">
<tr><td>Telephone:</td><td>+27 11 11 1111</td></tr>
<tr><td>Fax:</td><td>+27 11 111 2222</td></tr>
<tr><td>Email:</td><td>support#fubar.com</td></tr>
</table>
<p>We will automatically try to reconnect you in <span id="timeleft"></span> seconds</p>
</td>
</tr>
</table>
<script type="text/javascript">
var refreshEvery = 10;
var currentSec = 0;
var timer = setInterval(function() {
currentSec++;
if (currentSec >= refreshEvery) {
clearInterval(timer);
location.reload();
}
document.getElementById("timeleft").innerHTML = "" + (refreshEvery - currentSec);
}, 1000)
document.getElementById("timeleft").innerHTML = "" + (refreshEvery - currentSec);
// Use this site to create a base64 image http://www.base64-image.de/step-1.php
if (document.domain.indexOf("stacksnippets") >= 0) {
// Cusomise the site here, you can also show hide html content.
document.body.style.backgroundColor = "black";
document.body.style.color = "white";
} else {
// put default stuff here
}
</script>
</body>
</html>
Try to start out with a fresh and simple app_offline.htm file, like
<html><body>offline</body></html>
to see if that fixes the problem. In my case the problem was that the file encoding of the app_offline.htm file was "UTF-8 with BOM" rather than plain UTF-8.
The following handler also needs to be present:
ExtensionlessUrlHandler-Integrated-4.0
Make sure that in IIS Manager, on the website's Properties, the Application name has been created. Properties > Directory > Application settings > Application name.
Tested in IIS V6.0
None of the solutions above worked for us.
When testing on the server with same file but renamed (app_offline.TEMPLATE.htm) the server loaded the htm page without issue.
This was resolved by removing the large-ish Base64 encoded image from the file. (used as a background-image)
The app_offline.htm file still uses a Base64 encoded favicon and company logo image.

Intercept WordPress Admin Dashboard for Alerts

How can I make a plugin send an alert to one's dashboard at the top right below where the WordPress upgrade notice might normally appear?
I imagine I need some kind of add_filter() or add_action() call in the plugins' code.
Insert the following code in either a functions.php of your theme, or in a plugin. It's a sample that you can adapt and take further. In this demo, I wanted to demonstrate where you could post a message saying that the user needs to update a plugin.
<? function addDashboardAlert() { ?>
<style type="text/css">
.alert {
padding-top:4px;
padding-bottom:6px;
padding-left:302px;
background-color:#ebfbff;
border-bottom:1px solid #CCC;
display:none;
}
</style>
<script type="text/javascript">
$j = jQuery;
$j().ready(function(){ //when page has fully loaded
$j('h2:contains("Dashboard")').parent().prev().after('<div id="my-plugin-alert" class="alert">X Plugin 2.0 is available. Upgrade Now!</div>');
setTimeout("$j('#my-plugin-alert').fadeIn('slow');clearTimeout();",1000);
});
</script>
<? } add_action('admin_head','addDashboardAlert'); ?>
First, it intercepts admin_head to insert some Javascript. In the Javascript, since we know that current WordPress's include jQuery, but loads it with the .noConflict() option, then we can assign the jQuery to $j to keep it short in our code. When the page has fully loaded, it looks for an H2 that contains "Dashboard", meaning the Dashboard page. It then traverses up the DOM a little to a nice slot where it can insert the alert and then adds one there. It concludes with a professional fadeIn() call.