Inlining Data

To avoid content flash on initial application loading, you can embed an SQL query in your application html. When RdbHost.com sends the app html page to the browser, it scans for an embedded query, executes the query, and inlines the results, as JSON, into the html.

How To:

The embedded SQL is part of a script tag, as attribute 'sql' or 'data-sql'. The query is executed as the role given by the 'role' or 'data-role' attribute, by default the preauth role.. The script must have id 'RDBHOST-SQL-INLINE-ID', and be of a type that the browser will ignore. I suggest type 'text/sql'.

Usage Example

<script id="RDBHOST-SQL-INLINE-ID" type="text/sql" data-sql="SELECT * FROM samples; SELECT 1;"
        data-role=p0000000014></script>

<script>
  var p = Rdbhost.inline_sql();
  p.then(function(d) {
    var rows = d.result_rows[0].records.rows;
    // do something with rows data from 'samples' table
  })
</script>

If the query is not whitelisted the first time you load the page, the inline data will be the JSON error results, and the module will try to get the rows data via its regular ajaxy request, and you will be asked, via the usual dialog, to authorize whitelisting.

Usage Example without Library

You might want to include rendering code inline or in its own file, to avoid waiting for RdbHost module to load.

<script id="RDBHOST-SQL-INLINE-ID" type="text/sql" data-sql="SELECT * FROM samples"
data-role=p0000000014></script>

<script>
  var json = $('#RDBHOST-SQL-INLINE-ID').html(),
  data = JSON.parse(json),
  // do something with data
</script>

These examples will have records from the table samples included, as JSON in the usual RdbHost format, as the body of the script tag, when the page initially loads in the browser. The JavaScript segments read that body, convert it to JavaScript data, and do something with it, presumably passing it to a templater.

If the query errors, the error will be provided as the body, in the usual RdbHost JSON error format.

Example

This page (as do most public RdbHost pages) includes an example of inline-sql. Just view-source.

Benefits

  1. Initial page render is faster than waiting on an Ajax request.
  2. SEO is better, as data is in the inital page, and rendered quickly.

Caution

Only the first script tag with id 'RDBHOST-SQL-INLINE-ID' gets processed, and it gets processed even if inside an html comment. If you need to disable it, change attrubute name 'id' to something else, or change the <script..> tag itself to a comment.

Relevant Blog Posts